1 
2 #include <stdbool.h>
3 #include <string.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6 
7 #include "cJSON.h"
8 #include "dev_info.h"
9 #include "modnetwork.h"
10 #include "usb_s_uvc_ioctl.h"
11 #include "zbar.h"
12 
network_qrcode_scan(void * gray,int w,int h,char * ouput,int max_len)13 int network_qrcode_scan(void *gray, int w, int h, char *ouput, int max_len)
14 {
15     int ret = -1, len = 0;
16     char *data = NULL;
17     zbar_image_scanner_t *scanner = NULL;
18     zbar_image_t *image = NULL;
19     const zbar_symbol_t *symbol = NULL;
20     zbar_symbol_type_t type = ZBAR_NONE;
21 
22     do {
23         scanner = zbar_image_scanner_create();
24         if (NULL == scanner) {
25             break;
26         }
27         /* configure the reader */
28         zbar_image_scanner_set_config(scanner, ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
29 
30         /* wrap image data */
31         image = zbar_image_create();
32         if (NULL == image) {
33             break;
34         }
35 
36         zbar_image_set_format(image, *(int *)"Y800");
37         zbar_image_set_size(image, w, h);
38         zbar_image_set_data(image, gray, w * h, NULL);
39 
40         /* scan the image for barcodes */
41         ret = zbar_scan_image(scanner, image);
42         if (0 >= ret) {
43             break;
44         }
45 
46         symbol = zbar_image_first_symbol(image);
47         for (; symbol; symbol = zbar_symbol_next(symbol)) {
48             /* do something useful with results */
49             type = zbar_symbol_get_type(symbol);
50             if (ZBAR_QRCODE != type) {
51                 printf("zbar code ----> %s, skip\n",
52                        zbar_get_symbol_name(type));
53                 continue;
54             }
55 
56             data = (char *)zbar_symbol_get_data(symbol);
57             len = (unsigned int)zbar_symbol_get_data_length(symbol);
58 
59             if (data && max_len > len) {
60                 memcpy(ouput, data, len);
61                 ret = len;
62             }
63         }
64     } while (0);
65 
66     if (image) {
67         zbar_image_free_data(image);
68         zbar_image_destroy(image);
69     }
70 
71     if (scanner) {
72         zbar_image_scanner_destroy(scanner);
73     }
74     return ret;
75 }
76 
network_qrscan_result_process(const char * result,char * ssid,char * passwd)77 int network_qrscan_result_process(const char *result, char *ssid, char *passwd)
78 {
79     int ret = 0;
80     cJSON *ssid_elem, *passwd_elem;
81     cJSON *root = cJSON_Parse(result);
82     if (root == NULL) {
83         return 0;
84     }
85     // {"ssid":"aaaaa","pwd":"sssddddd"}
86     ssid_elem = cJSON_GetObjectItem(root, "ssid");
87     passwd_elem = cJSON_GetObjectItem(root, "pwd");
88 
89     do {
90         if (ssid_elem == NULL || passwd_elem == NULL)
91             break;
92 
93         if (!cJSON_IsString(ssid_elem) || !cJSON_IsString(passwd_elem))
94             break;
95 
96         if (strlen(ssid_elem->valuestring) > NETWORK_SSID_MAX_LEN ||
97             strlen(passwd_elem->valuestring) > NETWORK_PASSWD_MAX_LEN) {
98             break;
99         }
100 
101         strncpy(ssid, ssid_elem->valuestring, NETWORK_SSID_MAX_LEN);
102         strncpy(passwd, passwd_elem->valuestring, NETWORK_PASSWD_MAX_LEN);
103         ret = 1;
104     } while (0);
105 
106     cJSON_Delete(root);
107 
108     return ret;
109 }