• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <unistd.h>
17 #include <sys/time.h>
18 #include <stdio.h>
19 #include <hdf_log.h>
20 #include <hdf_remote_service.h>
21 #include <hdf_sbuf.h>
22 #include <servmgr_hdi.h>
23 #include "cdcacm.h"
24 
25 #define HDF_LOG_TAG hcs_prop
26 #define OPTION_EDN (-1)
27 #define ACM_SERVICE_NAME "usbfn_cdcacm"
28 
29 static struct HdfSBuf *g_data;
30 static struct HdfSBuf *g_reply;
31 static struct HdfRemoteService *g_acmService;
32 
ShowUsage()33 static void ShowUsage()
34 {
35     HDF_LOGE("Usage options:\n");
36     HDF_LOGE("-g : name of getting prop, as: -g idProduct");
37     HDF_LOGE("-s : name of setting prop, as: -s idProduct 0xa4b7");
38     HDF_LOGE("-r : regist prop, as: -r testa aaaaa");
39     HDF_LOGE("-h : show this help message");
40 }
41 
DispatcherInit(void)42 static int DispatcherInit(void)
43 {
44     struct HDIServiceManager *servmgr = HDIServiceManagerGet();
45     if (servmgr == NULL) {
46         HDF_LOGE("%s: HDIServiceManagerGet err", __func__);
47         return HDF_FAILURE;
48     }
49     g_acmService = servmgr->GetService(servmgr, ACM_SERVICE_NAME);
50     HDIServiceManagerRelease(servmgr);
51     if (g_acmService == NULL) {
52         HDF_LOGE("%s: GetService err", __func__);
53         return HDF_FAILURE;
54     }
55 
56     g_data = HdfSBufTypedObtain(SBUF_IPC);
57     g_reply = HdfSBufTypedObtain(SBUF_IPC);
58     if (g_data == NULL || g_reply == NULL) {
59         HDF_LOGE("%s: GetService err", __func__);
60         return HDF_FAILURE;
61     }
62     return HDF_SUCCESS;
63 }
64 
DispatcherDeInit(void)65 static void DispatcherDeInit(void)
66 {
67     HdfSBufRecycle(g_data);
68     HdfSBufRecycle(g_reply);
69 }
70 
TestPropGet(const char * propName)71 static int TestPropGet(const char *propName)
72 {
73     int status = -1;
74     const char *propVal = NULL;
75     if (!HdfSbufWriteString(g_data, propName)) {
76         HDF_LOGE("%s:failed to write result", __func__);
77         goto FAIL;
78     }
79     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_GET_PROP, g_data, g_reply);
80     if (status !=  HDF_SUCCESS) {
81         HDF_LOGE("%s: Dispatch USB_SERIAL_GET_PROP failed status = %d", __func__, status);
82         goto FAIL;
83     }
84     propVal = HdfSbufReadString(g_reply);
85     if (propVal == NULL) {
86         HDF_LOGE("%s:failed to write result", __func__);
87         goto FAIL;
88     }
89     HDF_LOGE("%s: %s = %s", __func__, propName, propVal);
90 
91 FAIL:
92     return status;
93 }
94 
TestPropSet(const char * propName,const char * propValue)95 static int TestPropSet(const char *propName, const char *propValue)
96 {
97     int status = -1;
98     if (!HdfSbufWriteString(g_data, propName)) {
99         HDF_LOGE("%s:failed to write propName : %s", __func__, propName);
100         goto FAIL;
101     }
102     if (!HdfSbufWriteString(g_data, propValue)) {
103         HDF_LOGE("%s:failed to write propValue : %s", __func__, propValue);
104         goto FAIL;
105     }
106     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_SET_PROP, g_data, g_reply);
107     if (status !=  HDF_SUCCESS) {
108         HDF_LOGE("%s: Dispatch USB_SERIAL_SET_PROP failed", __func__);
109     }
110  FAIL:
111     return status;
112 }
113 
TestPropRegist(const char * propName,const char * propValue)114 static int TestPropRegist(const char *propName, const char *propValue)
115 {
116     int status;
117 
118     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
119     if (status) {
120         HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
121         return HDF_FAILURE;
122     }
123     if (!HdfSbufWriteString(g_data, propName)) {
124     HDF_LOGE("%s:failed to write propName : %s", __func__, propName);
125     goto FAIL;
126     }
127     if (!HdfSbufWriteString(g_data, propValue)) {
128     HDF_LOGE("%s:failed to write propValue : %s", __func__, propValue);
129     goto FAIL;
130     }
131     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_REGIST_PROP, g_data, g_reply);
132     if (status !=  HDF_SUCCESS) {
133     HDF_LOGE("%s: Dispatch USB_SERIAL_SET_PROP failed status = %d", __func__, status);
134     }
135  FAIL:
136     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
137     if (status) {
138         HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
139         return HDF_FAILURE;
140     }
141 
142     return status;
143 }
144 
145 
main(int argc,char * argv[])146 int main(int argc, char *argv[])
147 {
148     int ch;
149     int ret;
150     const char *serviceName = NULL;
151     const char *propName = NULL;
152     const char *propValue = NULL;
153     bool setProp = false;
154     bool getProp = false;
155     bool registProp = false;
156 
157     while ((ch = getopt(argc, argv, "S:r:g:s:h?")) != OPTION_EDN) {
158         switch (ch) {
159             case 'S':
160                 serviceName = optarg;
161                 break;
162             case 'r':
163                 propName = optarg;
164                 propValue = argv[optind];
165                 registProp = true;
166                 break;
167             case 'g':
168                 propName = optarg;
169                 getProp = true;
170                 break;
171             case 's':
172                 propName = optarg;
173                 propValue = argv[optind];
174                 setProp = true;
175                 break;
176             case 'h':
177             case '?':
178                 ShowUsage();
179                 return 0;
180                 break;
181             default:
182                 break;
183         }
184     }
185 
186     if (DispatcherInit() != HDF_SUCCESS) {
187         return 1;
188     }
189     if (getProp) {
190         ret = TestPropGet(propName);
191     } else if (setProp) {
192     	ret = TestPropSet(propName, propValue);
193     } else if (registProp) {
194         ret = TestPropRegist(propName, propValue);
195     }
196     DispatcherDeInit();
197 
198     return 0;
199 }
200 
201