1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "wayland_drm_auth_server.h"
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include "xf86drm.h"
20 #include "wayland_drm_auth.h"
21 #include "drm-auth-server-protocol.h"
22
23 typedef struct {
24 int maserFd;
25 struct wl_global *drmAuthGlobal;
26 } DrmAuthMng;
27
28 DrmAuthMng *g_drmAuthMng = NULL;
29
DrmAuthenticate(struct wl_client * client,struct wl_resource * resource,uint32_t magic)30 static void DrmAuthenticate(struct wl_client *client, struct wl_resource *resource, uint32_t magic)
31 {
32 int ret;
33 DrmAuthMng *drmAuthMng = wl_resource_get_user_data(resource);
34 DRMAUTH_LOGD("DrmAuthenticate magic %x", magic);
35 CHK_RETURN_NO_VALUE((drmAuthMng == NULL), DRMAUTH_LOGE("can not get user data"));
36 ret = drmAuthMagic(drmAuthMng->maserFd, magic);
37 if (ret) {
38 DRMAUTH_LOGE("drm authenticate failed errno : %d", errno);
39 wl_resource_post_event(resource, WL_DRM_AUTH_STATUS, WL_DRM_AUTH_STATUS_FAILED);
40 return;
41 }
42 wl_resource_post_event(resource, WL_DRM_AUTH_STATUS, WL_DRM_AUTH_STATUS_SUCCESS);
43 }
44
45 static const struct wl_drm_auth_interface g_drmAuthInterface = { DrmAuthenticate };
46
BindDrmAuth(struct wl_client * client,void * data,uint32_t version,uint32_t id)47 static void BindDrmAuth(struct wl_client *client, void *data, uint32_t version, uint32_t id)
48 {
49 struct wl_resource *resource;
50 DRMAUTH_LOGD("BindDrmAuth");
51 resource = wl_resource_create(client, &wl_drm_auth_interface, 1, id);
52 CHK_RETURN_NO_VALUE((resource == NULL), DRMAUTH_LOGE("create resource failed"); wl_client_post_no_memory(client));
53 wl_resource_set_implementation(resource, &g_drmAuthInterface, data, NULL);
54 }
55
DeInitWaylandDrmAuthService()56 void DeInitWaylandDrmAuthService()
57 {
58 DRMAUTH_LOGD("DeInitWaylandDrmAuthService");
59 if (g_drmAuthMng->drmAuthGlobal != NULL) {
60 wl_global_destroy(g_drmAuthMng->drmAuthGlobal);
61 }
62 if (g_drmAuthMng != NULL) {
63 free(g_drmAuthMng);
64 g_drmAuthMng = NULL;
65 }
66 }
67
InitWaylandDrmAuthService(struct wl_display * display,int drmMasterFd)68 int InitWaylandDrmAuthService(struct wl_display *display, int drmMasterFd)
69 {
70 if (g_drmAuthMng != NULL) {
71 DRMAUTH_LOGI("drm auth service has inited will do nothing");
72 }
73 g_drmAuthMng = calloc(1, sizeof(DrmAuthMng));
74 CHK_RETURN((g_drmAuthMng == NULL), -1, DRMAUTH_LOGE("calloc DrmAuthMng Failed errno : %d", errno));
75 g_drmAuthMng->maserFd = drmMasterFd;
76 g_drmAuthMng->drmAuthGlobal = wl_global_create(display, &wl_drm_auth_interface, 1, g_drmAuthMng, BindDrmAuth);
77 CHK_RETURN((g_drmAuthMng == NULL), -1, DRMAUTH_LOGE("drm auth global create failed");
78 DeInitWaylandDrmAuthService());
79 return 0;
80 }
81