-
Notifications
You must be signed in to change notification settings - Fork 3
/
mfrlibs_rpi.c
340 lines (318 loc) · 11.5 KB
/
mfrlibs_rpi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2017 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <mfrMgr.h>
#include <mfrTypes.h>
#include <stdlib.h>
#include <string.h>
#include "mfr_wifi_types.h"
#include "mfr_wifi_api.h"
#define MAX_BUF_LEN 255
#define SIZE 50
#define MAC_ADDRESS_SIZE 12
#define SERIAL_MAX_SIZE 16
#define MAX_COMMAND_SIZE 100
//#define DEBUG
const char defaultManufacturer[] = "UNKNOWN MANUFACTURER";
const char defaultManufacturerOUI[] = "B827EB";
const char defaultModelName[] = "RASPBERRYPI_3";
const char defaultDescription[] = "RASPBERRYPI_3 mediaclient";
const char defaultProductClass[] = "RDK";
const char defaultSerialNumber[] = "0000000000000000";
const char defaultHardwareVersion[] = "RASPBERRYPI_3_V01";
const char defaultSoftwareVersion[] = "2.0";
const char defaultMacAddress[] = "000000000000";
void mfrlib_log(const char *format, ...)
{
int total = 0;
va_list args;
int buf_index;
#ifdef DEBUG
va_start(args, format);
// log to console
total = vfprintf(stdout, format, args);
fflush(stdout);
va_end(args);
#endif
}
void mfrFreeBuffer(char *buf)
{
if (buf)
free(buf);
}
mfrError_t mfrGetSerializedData(mfrSerializedType_t param, mfrSerializedData_t *data)
{
char cmd[MAX_COMMAND_SIZE];
char buffer[MAX_BUF_LEN];
FILE *fp = NULL;
mfrError_t ret = mfrERR_NONE;
if (!data)
return mfrERR_INVALID_PARAM;
data->freeBuf = mfrFreeBuffer;
switch (param)
{
case mfrSERIALIZED_TYPE_MANUFACTURER:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag MANUFACTURE from /etc/device.properties */
sprintf(cmd, "cat /etc/device.properties | grep MANUFACTURE | sed -e 's/.*=//g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultManufacturer);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, strlen(buffer) - 1);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("Manufacturer= %s\t len=%d\n", data->buf, data->bufLen);
break;
/* unique identifier of the Manufacturer :: we are using the first 6 chars of the mac address */
case mfrSERIALIZED_TYPE_MANUFACTUREROUI:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
sprintf(cmd, "ifconfig | grep `ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d' | head -n1` | tr -s ' ' | cut -d ' ' -f5 | sed -e 's/://g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultManufacturerOUI);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, 6);
data->bufLen = 6;
pclose(fp);
}
mfrlib_log("Manufacturer OUI= %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_MODELNAME:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag MODEL_NUM from /etc/device.properties */
sprintf(cmd, "cat /etc/device.properties | grep MODEL_NUM | sed -e 's/.*=//g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultModelName);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, strlen(buffer) - 1);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("Model Name= %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_DESCRIPTION:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag DEVICE_TYPE from /etc/device.properties */
sprintf(cmd, "cat /etc/device.properties | grep DEVICE_TYPE | sed -e 's/.*=//g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultDescription);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
sprintf(data->buf, "%s ", defaultModelName);
strncat(data->buf, buffer, strlen(buffer) - 1);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("DESCRIPTION= %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_PRODUCTCLASS:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag DEVICE_NAME from /etc/device.properties */
sprintf(cmd, "cat /etc/device.properties | grep DEVICE_NAME | sed -e 's/.*=//g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultProductClass);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, strlen(buffer) - 1);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("PRODUCT CLASS= %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_SERIALNUMBER:
case mfrSERIALIZED_TYPE_MANUFACTURING_SERIALNUMBER:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag Serial from /proc/cpuinfo */
sprintf(cmd, "cat /proc/cpuinfo | grep Serial | sed -e 's/.*: //g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultSerialNumber);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, SERIAL_MAX_SIZE);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("Serial Number = %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_HARDWAREVERSION:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag Revision from /proc/cpuinfo */
sprintf(cmd, "cat /proc/cpuinfo | grep Revision | sed -e 's/.*: //g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultHardwareVersion);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, strlen(buffer) - 1);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("Hardware Version= %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_SOFTWAREVERSION:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
/* retrieving tag BUILD_VERSION from /etc/device.properties */
sprintf(cmd, "cat /etc/device.properties | grep BUILD_VERSION | sed -e 's/.*=//g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultSoftwareVersion);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, strlen(buffer) - 1);
data->bufLen = strlen(data->buf);
pclose(fp);
}
mfrlib_log("Build Version = %s\t len=%d\n", data->buf, data->bufLen);
break;
case mfrSERIALIZED_TYPE_PROVISIONINGCODE:
case mfrSERIALIZED_TYPE_FIRSTUSEDATE:
case mfrSERIALIZED_TYPE_MOCAMAC:
case mfrSERIALIZED_TYPE_HDMIHDCP:
case mfrSERIALIZED_TYPE_MAX:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
strcpy(data->buf, "XXXX");
data->bufLen = strlen(data->buf);
break;
case mfrSERIALIZED_TYPE_DEVICEMAC:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
memset(cmd, 0, sizeof(char) * 100);
memset(buffer, 0, MAX_BUF_LEN);
sprintf(cmd, "ifconfig | grep `ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d' | head -n1` | tr -s ' ' | cut -d ' ' -f5 | sed -e 's/://g'");
if ((fp = popen(cmd, "r")) == NULL)
{
mfrlib_log("popen failed.");
strcpy(data->buf, defaultMacAddress);
data->bufLen = strlen(data->buf);
}
if (fp)
{
fgets(buffer, sizeof(buffer), fp);
strncpy(data->buf, buffer, MAC_ADDRESS_SIZE);
data->bufLen = MAC_ADDRESS_SIZE;
pclose(fp);
}
mfrlib_log("MAC Address = %s\t len=%d\n", data->buf, data->bufLen);
break;
default:
data->buf = (char *)malloc(sizeof(char) * MAX_BUF_LEN);
memset(data->buf, '\0', sizeof(char) * MAX_BUF_LEN);
data->bufLen = strlen(data->buf);
break;
}
return mfrERR_NONE;
}
WIFI_API_RESULT WIFI_GetCredentials(WIFI_DATA *pData)
{
if (pData == NULL)
{
return WIFI_API_RESULT_NULL_PARAM;
}
return WIFI_API_RESULT_OPERATION_NOT_SUPPORTED;
}
WIFI_API_RESULT WIFI_SetCredentials(WIFI_DATA *pData)
{
if (pData == NULL)
{
return WIFI_API_RESULT_NULL_PARAM;
}
return WIFI_API_RESULT_OPERATION_NOT_SUPPORTED;
}
mfrError_t mfrDeletePDRI()
{
return mfrERR_NONE;
}
mfrError_t mfrScrubAllBanks()
{
return mfrERR_NONE;
}
mfrError_t mfrWriteImage(const char *str, const char *str1,
mfrImageType_t imageType, mfrUpgradeStatusNotify_t upgradeStatus)
{
return mfrERR_NONE;
}
mfrError_t mfr_init()
{
return mfrERR_NONE;
}