summaryrefslogtreecommitdiffstats
path: root/package/query3g/src/apnprovider.c
blob: c5f812073ab7e035a2b610ba114d35977958bb9c (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>

#define FNAME "/usr/share/3gmodem/apn.json"

int main( int argc, char **argv )
{
	int ret=0;
	int cnt_apn=0;
	if ( argc != 2 )
	{
		printf(" %s [APN_Provider_5_symbols]\n", argv[0]);
		printf("%s 12345\n", argv[0]);
		return -1;
	} else if ( strlen(argv[1]) != 5 )
	{
		printf("ERR: Only 5 number apn supported\n");
		return -1;
	}
	
	char *apnnumber = argv[1];
	char mcc[4];
	char mnc[3];
	memcpy( mcc, apnnumber, 3 );
	mcc[3] = 0;
	memcpy( mnc, apnnumber+3 , 2 );
	mnc[2] = 0;
	
	int i;
	struct json_object *jo = json_object_from_file( FNAME );
	if ( jo == NULL )
	{
		printf("ERR:Cannot get file\n");
		ret = 1;
		goto free_resources;
	}
	
	struct json_object *jarr = json_object_object_get( jo, "data" );
	if ( jarr == NULL )
	{
		printf("ERR:Cannot get data object\n");
		ret = 1;
		goto free_resources;
	}
	
	if ( !json_object_is_type( jarr, json_type_array ) )
	{
		printf("ERR:Object isnot array\n");
		ret = 1;
		goto free_resources;
	}
	
	for (i=0; i<json_object_array_length(jarr); i++)
	{
		json_object *obj = json_object_array_get_idx(jarr, i);
		struct json_object *jmcc = json_object_object_get( obj, "mcc" );
		struct json_object *jmnc = json_object_object_get( obj, "mnc" );
		struct json_object *jname = json_object_object_get( obj, "name" );
		struct json_object *jfullname = json_object_object_get( obj, "fullname" );
		if ( (jmcc != NULL) && (jmnc != NULL) && ( (jname != NULL) || (jfullname != NULL) ) )
		{
			const char *tmp_mcc = json_object_to_json_string( jmcc );
			const char *tmp_mnc = json_object_to_json_string( jmnc );
			if ( strncmp( tmp_mcc+1, mcc, 3 ) != 0 )
			{
				continue;
			}
			if ( strncmp( tmp_mnc+1, mnc, 2 ) != 0 )
			{
				continue;
			}
			printf("%s ", tmp_mcc );
			printf("%s ", tmp_mnc );
			if ( jname != NULL )
				printf("%s ", json_object_to_json_string( jname ) );
			if ( jfullname != NULL )
				printf("%s ", json_object_to_json_string( jfullname ) );
			printf("\n");
			cnt_apn++;
		}
	}
free_resources:
	json_object_put( jo );
	
	if (cnt_apn<=0)
		ret = 1;
	return ret;
}