ajout d'un exemple de systeme d'autoconfiguration via http
[ipphone-tools.git] / ipphone5-phonebook.c
1 #include <unistd.h> /* read,write,close */
2 #include <string.h> /* str* */
3 #include <stdlib.h> /* atoi */
4 #include <stdio.h> /* FILE,fprintf */
5
6 #define SETTINGS_HINT "sh"
7
8 #define TOTAL_SETTINGS_SIZE 4096
9
10 #define SPEED_DIAL_BEGIN 0x0200
11 #define SPEED_DIAL_NUM 100
12 #define SPEED_DIAL_LEN 32
13 #define PHONE_NUMBER_LEN 16
14
15 void display_phonebook(FILE *filehandle, int start, int end) {
16 char buffer[SPEED_DIAL_LEN*SPEED_DIAL_NUM], *pos;
17 if (start < 0) start = 0;
18 if (end > (SPEED_DIAL_NUM - 1)) end = SPEED_DIAL_NUM - 1;
19 fseek(filehandle, SPEED_DIAL_BEGIN + (SPEED_DIAL_LEN * start), SEEK_SET);
20 pos = buffer + (SPEED_DIAL_LEN * start);
21 fread(pos, SPEED_DIAL_LEN, (end - start + 1), filehandle);
22 fprintf(stdout, "#index:number:name\n");
23 while (start <= end) {
24 // FIXME: should not be ".16" but something like ".%d",PHONE_NUMBER_LEN
25 fprintf(stdout, "%d:%.16s:%.16s\n", start, pos, pos + PHONE_NUMBER_LEN);
26 pos += SPEED_DIAL_LEN;
27 start++;
28 }
29 }
30
31 int change_phonebook(FILE *filehandle, int index, const char *number, const char *name) {
32 char buffer[SPEED_DIAL_LEN];
33 if (index < 0 || (SPEED_DIAL_NUM - 1) < index) return -1;
34 strncpy(buffer, number, PHONE_NUMBER_LEN);
35 strncpy(buffer + PHONE_NUMBER_LEN, name, SPEED_DIAL_LEN - PHONE_NUMBER_LEN);
36 fseek(filehandle, SPEED_DIAL_BEGIN + (SPEED_DIAL_LEN * index), SEEK_SET);
37 if (fwrite(buffer, SPEED_DIAL_LEN, 1, filehandle) != 1) return -2;
38 return 0;
39 }
40
41 int main(int argc, const char *argv[]) {
42 const char *progname, *filename, *extension;
43 FILE *filehandle;
44 size_t length;
45
46 // check arguments count
47 progname = argv[0];
48 if (argc < 2 || 5 < argc) {
49 fprintf(stderr, "Syntax: %s <filename> [<index>[-<end>] [<new-number> [<new-name>]]]\n", progname);
50 return 1;
51 }
52 filename = argv[1];
53
54 // check first argument (filename)
55 filehandle = fopen(filename, "r+b");
56 if (filehandle == NULL) {
57 fprintf(stderr, "Unable to open '%s' !\n", filename);
58 return 2;
59 }
60 // fprintf(stdout, "# File name: %s\n", filename);
61
62 // check file type (by extension, for now...)
63 extension = strrchr(filename, '.');
64 if (extension && strcasecmp(extension, ".sh")) {
65 fprintf(stderr, "Unknown file type!\n");
66 return 2;
67 }
68
69 // check file size
70 fseek(filehandle, 0L, SEEK_END);
71 length = ftell(filehandle);
72 if (length != TOTAL_SETTINGS_SIZE) {
73 fprintf(stderr, "Invalid file size (%d != %d) !\n", length, TOTAL_SETTINGS_SIZE);
74 return 3;
75 }
76 // fprintf(stdout, "# File length: %d bytes\n", length);
77
78 // proceed as requested
79 if (argc < 4) {
80 int start, end;
81 if (argc > 2) {
82 char *pos, tmp[8]; /* "NNN-NNN\0" */
83 strncpy(tmp, argv[2], sizeof(tmp));
84 tmp[sizeof(tmp)-1] = '\0';
85 pos = strchr(tmp, '-');
86 if (pos) {
87 *(pos++) = 0;
88 start = atoi(tmp);
89 end = *pos ? atoi(pos) : (SPEED_DIAL_NUM - 1);
90 } else {
91 start = end = atoi(tmp);
92 }
93 } else {
94 start = 0;
95 end = SPEED_DIAL_NUM - 1;
96 }
97 display_phonebook(filehandle, start, end);
98 } else {
99 int index;
100 const char *number, *name;
101 index = atoi(argv[2]);
102 number = argv[3];
103 name = (argc >= 5) ? argv[4] : "";
104 change_phonebook(filehandle, index, number, name);
105 }
106
107 // cleaning
108 fclose(filehandle);
109 return 0;
110 }