• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <inttypes.h>
21 #include "audio_internal.h"
22 #include "hdf_log.h"
23 #include "pnp_message_report.h"
24 
25 #define PNP_REPORT_MSG_FIELD_NUM 5
26 
AudioPnpDevPlugMsgDeSerialize(uint8_t * msgStr,struct PnpReportDevPlugMsg * devPlugMsg)27 static int32_t AudioPnpDevPlugMsgDeSerialize(uint8_t *msgStr, struct PnpReportDevPlugMsg *devPlugMsg)
28 {
29     int i;
30     char *stringTepm = NULL;
31     uint8_t buf[PNP_REPORT_MSG_FIELD_NUM - 1] = {0};
32 
33     if (msgStr == NULL) {
34         HDF_LOGE("[%{public}s]: Parameter error!", __func__);
35         return AUDIO_HAL_ERR_INTERNAL;
36     }
37     char strTemp[PNP_REPORT_MSG_LEN_MAX] = {0};
38     memcpy_s(strTemp, PNP_REPORT_MSG_LEN_MAX - 1, (char *)msgStr, strlen((char *)msgStr));
39 
40     stringTepm = strtok((char*)strTemp, ";");
41     if (stringTepm != NULL) {
42         devPlugMsg->eventType = (uint8_t)atoi(stringTepm);
43         if (devPlugMsg->eventType != DEVICE_PULG) {
44             HDF_LOGE("[%{public}s]: PnpReportType error!", __func__);
45 
46             return AUDIO_HAL_ERR_INVALID_PARAM;
47         }
48     }
49     for (i = 1; i < PNP_REPORT_MSG_FIELD_NUM; i++) {
50         stringTepm = strtok(NULL, ";");
51         if (stringTepm != NULL) {
52             buf[i - 1] = (uint8_t)atoi(stringTepm);
53         } else {
54             HDF_LOGE("[%{public}s]: Parse error!", __func__);
55             return AUDIO_HAL_ERR_NOT_SUPPORT;
56         }
57     }
58     devPlugMsg->state = buf[0];
59     devPlugMsg->deviceType = buf[1];
60     devPlugMsg->deviceCap = buf[2]; // 2 is deviceCap
61     devPlugMsg->id = buf[3]; // 3 is id
62 
63     return AUDIO_HAL_SUCCESS;
64 }
65 
AudioPnpDevEventMsgDeSerialize(uint8_t * msgStr,struct PnpReportEventMsg * eventMsg)66 static int32_t AudioPnpDevEventMsgDeSerialize(uint8_t *msgStr, struct PnpReportEventMsg *eventMsg)
67 {
68     int i;
69     char *stringTepm = NULL;
70     uint8_t buf[PNP_REPORT_MSG_FIELD_NUM - 1] = {0};
71 
72     if (msgStr == NULL || eventMsg == NULL) {
73         HDF_LOGE("[%{public}s]: Parameter error!", __func__);
74         return AUDIO_HAL_ERR_INTERNAL;
75     }
76     char strTemp[PNP_REPORT_MSG_LEN_MAX] = {0};
77     memcpy_s(strTemp, PNP_REPORT_MSG_LEN_MAX - 1, (char *)msgStr, strlen((char *)msgStr));
78 
79     stringTepm = strtok((char *)strTemp, ";");
80     if (stringTepm != NULL) {
81         eventMsg->eventType = (uint8_t)atoi(stringTepm);
82         if (eventMsg->eventType != EVENT_REPORT) {
83             HDF_LOGE("[%{public}s]: PnpReportType error!", __func__);
84             return AUDIO_HAL_ERR_INVALID_PARAM;
85         }
86     }
87     for (i = 1; i < PNP_REPORT_MSG_FIELD_NUM; i++) {
88         stringTepm = strtok(NULL, ";");
89         if (stringTepm != NULL) {
90             buf[i - 1] = (uint8_t)atoi(stringTepm);
91         } else {
92             HDF_LOGE("[%{public}s]: Parse error!", __func__);
93             return AUDIO_HAL_ERR_NOT_SUPPORT;
94         }
95     }
96 
97     eventMsg->eventId = buf[0];
98     eventMsg->eventValue = buf[1];
99     eventMsg->deviceType = buf[2]; // 2 is deviceType
100     eventMsg->reserve = buf[3]; // 3 is reserve
101 
102     return AUDIO_HAL_SUCCESS;
103 }
104 
PnpReportMsgDeSerialize(uint8_t * msgStr,enum PnpReportType msgType,struct PnpReportMsg * pnpReportMsg)105 int32_t PnpReportMsgDeSerialize(uint8_t *msgStr, enum PnpReportType msgType,
106     struct PnpReportMsg *pnpReportMsg)
107 {
108     int32_t ret;
109     int len;
110     if (pnpReportMsg == NULL || msgStr == NULL) {
111         HDF_LOGE("[%{public}s]: Parameter error!", __func__);
112         return AUDIO_HAL_ERR_INVALID_PARAM;
113     }
114 
115     len = strlen((const char *)msgStr);
116     if (len == 0 || len > PNP_REPORT_MSG_LEN_MAX) {
117         HDF_LOGE("[%{public}s]: Parameter error!", __func__);
118         return AUDIO_HAL_ERR_INVALID_PARAM;
119     }
120 
121     switch (msgType) {
122         case DEVICE_PULG:
123             pnpReportMsg->reportType = DEVICE_PULG;
124             ret = AudioPnpDevPlugMsgDeSerialize(msgStr, &pnpReportMsg->devPlugMsg);
125             if (ret != 0) {
126                 HDF_LOGE("[%{public}s]: PnpDevPlugMsgPrase error!", __func__);
127                 return AUDIO_HAL_ERR_INTERNAL;
128             }
129             break;
130         case EVENT_REPORT:
131             pnpReportMsg->reportType = EVENT_REPORT;
132             ret = AudioPnpDevEventMsgDeSerialize(msgStr, &pnpReportMsg->eventMsg);
133             if (ret != 0) {
134                 HDF_LOGE("[%{public}s]: PnpDevEventMsgPrase error!", __func__);
135                 return AUDIO_HAL_ERR_INTERNAL;
136             }
137             break;
138         default:
139             HDF_LOGE("[%{public}s]: Unknown message type!", __func__);
140             return AUDIO_HAL_ERR_NOT_SUPPORT;
141     }
142     return AUDIO_HAL_SUCCESS;
143 }
144 
PnpReportMsgSerialize(struct PnpReportMsg * pnpReportMsg)145 char *PnpReportMsgSerialize(struct PnpReportMsg *pnpReportMsg)
146 {
147     int ret = 0;
148     char *msgBuf = NULL;
149     if (pnpReportMsg == NULL) {
150         HDF_LOGE("[%{public}s]: Parameter error!", __func__);
151         return NULL;
152     }
153     msgBuf = OsalMemCalloc(PNP_REPORT_MSG_LEN_MAX);
154     if (msgBuf == NULL) {
155         HDF_LOGE("[%{public}s]: Malloc memory failed!", __func__);
156         return NULL;
157     }
158 
159     switch (pnpReportMsg->reportType) {
160         case DEVICE_PULG:
161             ret = snprintf_s(msgBuf, PNP_REPORT_MSG_LEN_MAX, PNP_REPORT_MSG_LEN_MAX - 1, "%d;%d;%d;%d;%d",
162                              pnpReportMsg->devPlugMsg.eventType, pnpReportMsg->devPlugMsg.state,
163                              pnpReportMsg->devPlugMsg.deviceType, pnpReportMsg->devPlugMsg.deviceCap,
164                              pnpReportMsg->devPlugMsg.id);
165             break;
166         case EVENT_REPORT:
167             ret = snprintf_s(msgBuf, PNP_REPORT_MSG_LEN_MAX, PNP_REPORT_MSG_LEN_MAX - 1, "%d;%d;%d;%d;%d",
168                              pnpReportMsg->eventMsg.eventType, pnpReportMsg->eventMsg.eventId,
169                              pnpReportMsg->eventMsg.eventValue, pnpReportMsg->eventMsg.deviceType,
170                              pnpReportMsg->eventMsg.reserve);
171             break;
172         default:
173             HDF_LOGE("[%{public}s]: Unknown message type!", __func__);
174             OsalMemFree(msgBuf);
175             return NULL;
176     }
177     if (ret < 0) {
178         HDF_LOGE("[%{public}s]: snprintf_s failed!", __func__);
179         OsalMemFree(msgBuf);
180         return NULL;
181     }
182     return msgBuf;
183 }
184 
185