Newer
Older
/***************************************************************************
*
* Project: dongle
*
*
***************************************************************************/
//global varibales
char *home_url = "http://192.168.8.1/html/index.html";
char *sess_tok_url = "http://192.168.8.1/api/webserver/SesTokInfo";
//free mem.
char *parser(char *data, char *keyword)
{
char *session_token;
char *ptr = NULL;
session_token = (char *)calloc(1024, sizeof(*session_token));
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
if (!session_token)
goto leave_token;
//parse keyword..
ptr = strstr(data, keyword);
if (ptr == NULL)
{
goto leave_not_found;
}
int strl = strlen(keyword);
ptr += strl;
sscanf(ptr, "%[^<]", session_token);
return session_token;
leave_not_found:
free(session_token);
leave_token:
return NULL;
}
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t total_size = size * nmemb;
struct write_result *result = (struct write_result *)stream;
if (result->pos + total_size >= BUFFER_SIZE - 1) {
fprintf(stderr, "error: too small buffer\n");
return 0;
}
memcpy(result->data + result->pos, ptr, total_size);
result->pos += total_size;
return total_size;
}
struct write_result *memalloc_write_result(void)
{
struct write_result *wr = (struct write_result *) calloc(1, sizeof(*wr));
wr->data = calloc(BUFFER_SIZE, sizeof(wr->data));
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
if (!wr->data)
goto leave;
wr->pos = 0;
return wr;
leave:
if (wr)
free(wr);
if (wr->data)
free(wr->data);
return NULL;
}
void free_write_result(struct write_result *result)
{
if (result) {
free(result);
}
}
int check_cookie_status(CURLcode status, CURL *curl)
{
long code;
if (status != 0) {
if (DEBUG)
printf(": %s\n", curl_easy_strerror(status));
return 1;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if (code != 200) {
if (DEBUG)
printf("error: server responded with code %ld\n", code);
return 1;
}
return 0;
}
void prepare_curl_write_callback(CURL *curl, const char *url, struct write_result *result)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, result);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
}
//free mem
static struct write_result *get_request(const char *url, const char *api_url)
{
CURL *curl = NULL;
CURLcode status;
struct write_result *result;
curl = curl_easy_init();
if (!curl)
goto leave;
result = memalloc_write_result();
if (!result)
goto leave;
prepare_curl_write_callback(curl, url, result);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); //start the cookie engine ..
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt"); //Save the cookie from the server to cookie.txt
status = curl_easy_perform(curl);
if (check_cookie_status(status, curl))
goto leave;
//Prepare result struct to get next http response.
memset(result->data, 0, BUFFER_SIZE);
result->pos = 0;
prepare_curl_write_callback(curl, api_url, result);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt"); //Read locally stored cookies
status = curl_easy_perform(curl);
if (check_cookie_status(status, curl))
goto leave;
result->data[result->pos] = '\0';
if (DEBUG_RESULT)
printf("Http get request data is: %s\n", result->data);
curl_easy_cleanup(curl);
return result;
leave:
if (curl) {
printf("error occured: easy cleanup curl");
curl_easy_cleanup(curl);
}
if (result)
free_write_result(result);
return NULL;
}
//free mem
static struct write_result *post_request(const char *url, const char *api_url, char *post_query)
{
char token[1024] = {0};
char session[1024] = {0};
char *token_text = "__RequestVerificationToken:";
char *session_text = "Cookie:SessionID=";
struct curl_slist *headers = NULL;
struct write_result *result;
CURL *curl = NULL;
CURLcode status;
curl = curl_easy_init();
if (!curl)
goto leave;
result = memalloc_write_result();
if (!result)
goto leave;
prepare_curl_write_callback(curl, url, result);
status = curl_easy_perform(curl);
if (check_cookie_status(status, curl))
goto leave;
result->data[result->pos] = '\0';
char *session_id = parser(result->data, "SessionID=");
if (DEBUG)
printf("Session id is: %s\n", session_id);
char *token_id = parser(result->data, "<TokInfo>");
if (DEBUG)
printf("Token is: %s\n", token_id);
//clean the memory to prepare write operation for next http response..
memset(result->data, 0, BUFFER_SIZE);
result->pos = 0;
//prepare http header with token & session.
sprintf(token, "%s%s", token_text, token_id);
sprintf(session, "%s%s", session_text, session_id);
headers = curl_slist_append(headers, token);
headers = curl_slist_append(headers, session);
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
prepare_curl_write_callback(curl, api_url, result);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
status = curl_easy_perform(curl);
if (check_cookie_status(status, curl))
goto leave;
curl_easy_cleanup(curl);
result->data[result->pos] = '\0';
if (DEBUG_RESULT)
printf("http post request data is: %s\n", result->data);
//free the parser memeory..
if (token_id)
{
free(token_id);
token_id = NULL;
}
if (session_id)
{
free(session_id);
session_id = NULL;
}
return result;
leave:
if (curl) {
printf("error occured: easy cleanup curl\n");
curl_easy_cleanup(curl);
}
if (result)
free_write_result(result);
if (headers)
curl_slist_free_all(headers);
if (token_id)
free(token_id);
if (session_id)
free(session_id);
return NULL;
}
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
//pci, sc, cell_id, rsrq, rsrp, rssi, sinr, rscp, ecio, mode..
struct json_object *mobile_get_device_signal_hilink()
{
char *api_url = "http://192.168.8.1/api/device/signal";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
}
struct json_object *mobile_get_device_info_hilink()
{
char *api_url = "http://192.168.8.1/api/device/information";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_monitoring_status_hilink()
{
char *api_url = "http://192.168.8.1/api/monitoring/status";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_trafficstatistics_hilink()
{
char *api_url = "http://192.168.8.1/api/monitoring/traffic-statistics";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_provider_hilink()
{
char *api_url = "http://192.168.8.1/api/net/current-plmn";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_pin_status_hilink()
{
char *api_url = "http://192.168.8.1/api/pin/status";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_connection_status_hilink()
{
char *api_url = "http://192.168.8.1/api/dialup/connection";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_apn_profiles_hilink()
{
char *api_url = "http://192.168.8.1/api/dialup/profiles";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_notification_hilink()
{
char *api_url = "http://192.168.8.1/api/monitoring/check-notifications";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_sms_count_hilink()
{
char *api_url = "http://192.168.8.1/api/sms/sms-count";
struct write_result *result;
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_get_language_hilink()
{
char *api_url = "http://192.168.8.1/api/language/current-language";
struct json_object *result_json;
result = get_request(home_url, api_url);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_create_apn_profile_hilink(char *name, char *apn_name, char *username, char *password)
{
char *api_url = "http://192.168.8.1/api/dialup/profiles";
char post_query[1024];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 1023, "<request><Delete>0</Delete><SetDefault>0</SetDefault><Modify>1</Modify><Profile><Index></Index><IsValid>1</IsValid><Name>%s</Name><ApnIsStatic>1</ApnIsStatic><ApnName>%s</ApnName><DialupNum>*99#</DialupNum><Username>%s</Username><Password>%s</Password><AuthMode>0</AuthMode><IpIsStatic></IpIsStatic><IpAddress></IpAddress><DnsIsStatic></DnsIsStatic><PrimaryDns></PrimaryDns><SecondaryDns></SecondaryDns><ReadOnly>0</ReadOnly><iptype>2</iptype></Profile></request>", name, apn_name, username, password);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_set_apn_profile_hilink(char *name, char *apn_name, char *username, char *password)
{
char *api_url = "http://192.168.8.1/api/dialup/profiles";
char post_query[1024];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 1023, "<request><Delete>0</Delete><SetDefault>3</SetDefault><Modify>2</Modify><Profile><Index>3</Index><IsValid>1</IsValid><Name>%s</Name><ApnIsStatic>1</ApnIsStatic><ApnName>%s</ApnName><DialupNum>*99#</DialupNum><Username>%s</Username><Password>%s</Password><AuthMode>0</AuthMode><IpIsStatic>0</IpIsStatic><IpAddress></IpAddress><DnsIsStatic>0</DnsIsStatic><PrimaryDns></PrimaryDns><SecondaryDns></SecondaryDns><ReadOnly>0</ReadOnly><iptype>0</iptype></Profile></request>", name, apn_name, username, password);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_delete_apn_profile_hilink(int profile_name_location)
{
char *api_url = "http://192.168.8.1/api/dialup/profiles";
char post_query[512];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 512, "<request><Delete>%d</Delete><SetDefault>1</SetDefault><Modify>0</Modify></request>", profile_name_location);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_sms_read_hilink(int sms_location)
{
char *api_url = "http://192.168.8.1/api/sms/set-read";
char post_query[256];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 256, "<request><Index>%d</Index></request>", sms_location);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_sms_delete_hilink(int sms_location)
{
char *api_url = "http://192.168.8.1/api/sms/delete-sms";
char post_query[256];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 256, "<request><Index>%d</Index></request>", sms_location);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_reset_traffic_hilink()
{
char *api_url = "http://192.168.8.1/api/monitoring/clear-traffic";
char *post_query = "<request><ClearTraffic>1</ClearTraffic></request>";
struct write_result *result;
struct json_object *result_json;
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
}
/* mobile_LTE_register has to be called to enbale or disable LTE */
struct json_object *mobile_LTE_register_hilink()
{
char *api_url = "http://192.168.8.1/api/net/register";
char *post_query = "<request><Mode>0</Mode><Plmn></Plmn><Rat></Rat></request>";
struct write_result *result;
struct json_object *result_json;
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_LTE_enable_hilink()
{
char *api_url = "http://192.168.8.1/api/net/net-mode";
char *post_query = "<request><NetworkMode>00</NetworkMode><NetworkBand>3FFFFFFF</NetworkBand><LTEBand>7FFFFFFFFFFFFFFF</LTEBand></request>";
struct write_result *result;
struct json_object *result_json;
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_LTE_disable_hilink()
{
char *api_url = "http://192.168.8.1/api/net/net-mode";
char *post_query = "<NetworkMode>0201</NetworkMode><NetworkBand>3FFFFFFF</NetworkBand><LTEBand>7FFFFFFFFFFFFFFF</LTEBand></request>";
struct write_result *result;
struct json_object *result_json;
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
}
/* 00 - default- 4g, 01 - 2g, 02 - 3g */
struct json_object *mobile_set_connection_type_hilink(int connection_type)
{
char *api_url = "http://192.168.8.1/api/dialup/connection";
char post_query[256];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 256, "<request><NetworkMode>%d</NetworkMode><NetworkBand>3FFFFFFF</NetworkBand><LTEBand>7FFFFFFFFFFFFFFF</LTEBand></request>", connection_type);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *data_roaming(int action)
{
char *api_url = "http://192.168.8.1/api/dialup/connection";
char post_query[256];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 256, "<request><RoamAutoConnectEnable>%d</RoamAutoConnectEnable><MaxIdelTime>600</MaxIdelTime><ConnectMode>0</ConnectMode><MTU>1500</MTU><auto_dial_switch>1</auto_dial_switch><pdp_always_on>0</pdp_always_on></request>", action);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_enable_roaming_hilink()
struct json_object *mobile_disable_roaming_hilink()
{
return data_roaming(DISABLE);
}
/* 00 -> enter pin, 01 -> activate pin, 02 ->disable pin, 03 -> change pin, 04 -> enter puk */
struct json_object *pin_action(char *type, char *current_pin, char *new_pin, char *puk)
{
char *api_url = "http://192.168.8.1/api/pin/operate";
char post_query[256];
struct write_result *result;
struct json_object *result_json;
sprintf(post_query, 256, "<request><OperateType>%s</OperateType><CurrentPin>%s</CurrentPin><NewPin>%s</NewPin><PukCode>%s</PukCode></request>", type, current_pin, new_pin, puk);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *mobile_enable_pin_hilink(char *current_pin)
{
return pin_action("01", current_pin, "", "");
}
struct json_object *mobile_disable_pin_hilink(char *current_pin)
{
return pin_action("02", current_pin, "", "");
}
struct json_object *mobile_set_pin_hilink(char *current_pin, char *new_pin)
{
return pin_action("03", current_pin, new_pin, "");
}
struct json_object *mobile_unlock_sim_hilink(char *pin, char *puk)
{
return pin_action("4", pin, pin, puk);
}
struct json_object *mobile_set_language_hilink(char *language_name)
{
char *api_url = "http://192.168.8.1/api/language/current-language";
char post_query[128];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 128, "<request><CurrentLanguage>%s</CurrentLanguage></request>", language_name);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
debug_print("Json object inside library: %s\n", json_object_to_json_string(xml_to_json_converter(result)));
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)
free(result);
return result_json;
fail_converter:
free(result);
fail_result:
return NULL;
struct json_object *network_action(int action)
{
char *api_url = "http://192.168.8.1/api/dialup/dial";
char post_query[128];
struct write_result *result;
struct json_object *result_json;
snprintf(post_query, 128, "<request><Action>%d</Action></request>", action);
result = post_request(sess_tok_url, api_url, post_query);
if (!result)
goto fail_result;
result_json = xml_to_json_converter(result);
if(!result_json)
goto fail_converter;
if (result)