1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <signal.h>
5#include <fcntl.h>
6#include <netinet/in.h>
7#include <sys/types.h>
8#include <sys/socket.h>
9#include <net/if.h>
10#include <bcmnvram.h>
11#include <shutils.h>
12#include <wlutils.h>
13#include <syslog.h>
14#include "iboxcom.h"
15
16#define MAXARRAY 64
17#define MAXARRSIZE  64
18
19#ifdef NOPARSER
20int
21parse_array(char *file, char *func, char *strarr)
22{
23	FILE *fp;
24	char line[1024], *s, *l;
25	int flag=0, len;
26	int i;
27
28	if(!(fp=fopen(file, "r+"))) return 0;
29
30	while(fgets(line, sizeof(line), fp))
31	{
32		if(strstr(line, func)) flag=1;
33		if(flag && strstr(line, "return"))
34		{
35			flag=2;
36			break;
37		}
38	}
39	fclose(fp);
40
41	if (flag!=2) return 0;
42
43	l = strchr(line, ']');
44	if(l) *l=0;
45	l = strchr(line, '[');
46	l++;
47	s=l;
48	len = strlen(s);
49	while (strsep(&s, ","));
50
51	i=0;
52	/* Set token values */
53	for (s = l; s < &l[len] && *s; s+=strlen(s)+1)
54	{
55		strcpy(&strarr[i*MAXARRSIZE], s);
56		i++;
57
58		if(i==MAXARRAY) break;
59	}
60	return i;
61}
62
63int isClaimedIDE(char *model)
64{
65	char tmparray[MAXARRAY][MAXARRSIZE];
66	int tmpidx;
67	unsigned long size=0;
68	int i;
69
70	tmpidx=parse_array("/tmp/diskstatus", "claimed_disk_interface_names", tmparray);
71
72	for(i=0;i<tmpidx;i++)
73	{
74		printf("%d:%s\n", i, &tmparray[i][0]);
75		if (strstr(&tmparray[i][0], "IDE")) break;
76	}
77
78	if (i==tmpidx) return 0;
79
80	tmpidx=parse_array("/tmp/diskstatus", "claimed_disk_total_size", tmparray);
81	if (i>=tmpidx) return 0;
82	size = atoi(&tmparray[i][0]);
83
84	tmpidx=parse_array("/tmp/diskstatus", "claimed_disk_model_info", tmparray);
85	if (i>=tmpidx) return 0;
86	strcpy(model, &tmparray[i][0]);
87	return size;
88}
89
90int isBlankIDE(char *model)
91{
92	char tmparray[MAXARRAY][MAXARRSIZE];
93	int tmpidx;
94	unsigned long size=0;
95	int i;
96
97	tmpidx=parse_array("/tmp/diskstatus", "blank_disk_interface_names", tmparray);
98
99	for(i=0;i<tmpidx;i++)
100	{
101		printf("%d:%s\n", i, &tmparray[i][0]);
102		if (strstr(&tmparray[i][0], "IDE")) break;
103	}
104
105	if (i==tmpidx) return 0;
106
107	tmpidx=parse_array("/tmp/diskstatus", "blank_disk_total_size", tmparray);
108	if (i>=tmpidx) return 0;
109	size = atoi(&tmparray[i][0]);
110
111	tmpidx=parse_array("/tmp/diskstatus", "blank_disk_model_info", tmparray);
112	if (i>=tmpidx) return 0;
113	strcpy(model, &tmparray[i][0]);
114	return size;
115}
116
117
118int isPrinter(char *model1, char *model2)
119{
120	char tmparray[MAXARRAY][MAXARRSIZE];
121	int tmpidx;
122	unsigned long size=0;
123	int i;
124
125	tmpidx=parse_array("/tmp/diskstatus", "printer_models", tmparray);
126	if (tmpidx<=0) return 0;
127
128	if (tmpidx>=1)
129		strcpy(model1, &tmparray[0][0]);
130	if (tmpidx>=2)
131		strcpy(model2, &tmparray[1][0]);
132
133	return tmpidx;
134}
135#endif
136
137int
138getStorageStatus(STORAGE_INFO_T *st)
139{
140	char tmparray[MAXARRAY][MAXARRSIZE];
141
142	st->Capability=0;
143
144	unsigned long apps_status=0;
145
146	// count apps_status
147	if (nvram_match("swap_on", "1"))
148	{
149		apps_status|=APPS_STATUS_SWAP;
150	}
151	if (nvram_match("apps_comp", "1"))
152	{
153		apps_status|=APPS_STATUS_FILECOMPLETENESS;
154	}
155	if (nvram_match("apps_disk_free", "1"))
156	{
157		apps_status|=APPS_STATUS_FREESPACE;
158	}
159	if (nvram_match("ftp_running", "1") && nvram_match("st_samba_mode_x", "1"))
160	{
161		apps_status|=APPS_STATUS_SAMBAMODE;
162	}
163	if (nvram_match("apps_running", "1") && nvram_match("apps_dl_x", "1"))
164	{
165		apps_status|=APPS_STATUS_RUNNING;
166	}
167	if (nvram_invmatch("usb_disc0_dev", "") || nvram_invmatch("usb_disc1_dev", ""))
168	{
169		apps_status|=APPS_STATUS_DISCONPORT;
170	}
171	if (nvram_match("apps_dms_usb_port_x", "2"))
172	{
173		apps_status|=APPS_STATUS_DMPORT;
174	}
175	if (nvram_match("apps_dlx", "1"))
176	{
177		apps_status|=APPS_STATUS_DMMODE;
178	}
179	if (nvram_match("apps_status_checked", "1"))
180	{
181		apps_status|=APPS_STATUS_CHECKED;
182	}
183	if ( (nvram_match("usb_disc0_port", "1") || nvram_match("usb_disc0_port", "2")) &&
184	     (strcmp(nvram_safe_get("usb_disc0_port"), nvram_safe_get("usb_disc1_port"))!=0)
185	   )
186	{
187		apps_status|=APPS_STATUS_USBPORT1;
188	}
189	if ( (nvram_match("usb_disc1_port", "1") || nvram_match("usb_disc1_port", "2")) &&
190	     (strcmp(nvram_safe_get("usb_disc0_port"), nvram_safe_get("usb_disc1_port"))!=0)
191	   )
192	{
193		apps_status|=APPS_STATUS_USBPORT2;
194	}
195	st->AppsStatus=apps_status;
196
197	if(nvram_invmatch("apps_pool", "") && nvram_invmatch("apps_share", ""))
198	{
199		st->Capability = (unsigned long) atoi(nvram_safe_get("apps_caps"));
200		strcpy(st->AppsPool, nvram_safe_get("apps_pool"));
201		strcpy(st->AppsShare, nvram_safe_get("apps_share"));
202
203		strcpy(st->DiskModel, nvram_safe_get("apps_version"));	// 2008.09 James. for apps_version.
204	}
205	else
206	{
207		st->Capability=0;
208		st->AppsPool[0]=0;
209		st->AppsShare[0]=0;
210
211		st->DiskModel[0]=0;	// 2008.09 James. for apps_version.
212	}
213
214	st->DiskSize=0;
215	st->DiskStatus=DISK_STATUS_NONE;
216	//st->DiskModel[0]=0;	// 2008.09 James. for apps_version.
217	st->PrinterModel1[0]=0;
218	st->PrinterModel2[0]=0;
219
220#ifdef NOPARSER
221	// get from page
222	eval("wget", "http://127.0.0.1/diskstatus.asp", "-O", "/tmp/diskstatus");
223	// find claimed ide
224	if ((st->DiskSize=isClaimedIDE(st->DiskModel)))
225	{
226		st->DiskStatus=DISK_STATUS_CLAIMED;
227	}
228	else if ((st->DiskSize=isBlankIDE(st->DiskModel)))
229	{
230		st->DiskStatus=DISK_STATUS_BLANK;
231	}
232	isPrinter(st->PrinterModel1, st->PrinterModel2);
233#endif
234
235	//printf("Storage: %d %s\n", st->DiskStatus, st->AppsShare);
236	return 0;
237}
238
239