blob: d74b7e45dd0ab5d11c887ca249570db1c17b2a67 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/un.h>
6#include <sys/ioctl.h>
7#include <sys/socket.h>
8#include <linux/types.h>
9#include <linux/netlink.h>
10#include <errno.h>
11#include <unistd.h>
12#include <arpa/inet.h>
13#include <netinet/in.h>
14#include <syslog.h>
15#include <sys/klog.h>
16#include <signal.h>
17#include "sd_httpshare.h"
18#include "hotplug.h"
19
20
21//#define UEVENT_BUFFER_SIZE 1024*6
22//#define SD_ONLINE_MESSAGE "add@/devices/platform/zx297520_mmc.0/mmc_host/mmc0/mmc0"
23//#define SD_OFFLINE_MESSAGE "remove@/devices/platform/zx297520_mmc.0/mmc_host/mmc0/mmc0"
24#define SD_ONLINE_MESSAGE "add@/devices/platform/zx29_sd.1"
25#define SD_OFFLINE_MESSAGE "remove@/devices/platform/zx29_sd.1"
26#define PATH_LENGTH 256
27char usb_lun_path[PATH_LENGTH] = {0};
28
29/*¶ÁдÎļþÏà¹Ø²Ù×÷begin*/
30int filelength(FILE *fp)
31{
32 int num;
33 fseek(fp,0,SEEK_END);
34 num=ftell(fp);
35 fseek(fp,0,SEEK_SET);
36 return num;
37}
38
39static int readfile(char *path, char* buf, unsigned len)
40{
41 FILE *fp;
42 unsigned int length;
43 if((fp=fopen(path,"r"))==NULL)
44 {
45 slog(SDCARD_PRINT, SLOG_ERR, "open file %s error.\n",path);
46 return -1;
47 }
48 length=filelength(fp);
49 length = length > len? len: length;
50 //ch=(char *)malloc(length+1);
51 int read_len = fread(buf,length,1,fp);
52 if(read_len < 1) // cov M CHECKED_RETURN
53 {
54 slog(SDCARD_PRINT, SLOG_ERR, "fread %s error.\n",path);
55 }
56 fclose(fp);
57 *(buf+length)='\0';
58 return (int)length;
59}
60
61int sd_get_cdrom()
62{
63 char cdrom0[8] = {0};
64 char cdrom1[8] = {0};
65 int rtv = 0;
66 int cdrom_state0 = -1;
67 int cdrom_state1 = -1;
68
69 rtv = readfile("/sys/devices/platform/zx29_hsotg.0/gadget/lun0/cdrom", cdrom0, sizeof(cdrom0)-1);
70 cdrom_state0 = atoi(cdrom0);
71 rtv = readfile("/sys/devices/platform/zx29_hsotg.0/gadget/lun1/cdrom", cdrom1, sizeof(cdrom1)-1);
72 cdrom_state1 = atoi(cdrom1);
73
74 if (cdrom_state0 == 0) //lun0ΪuÅÌ
75 {
76 return 0;
77 }
78
79 if (cdrom_state1 == 0) //lun1ΪuÅÌ
80 {
81 return 1;
82 }
83
84 return -1;
85
86}
87
88static int init_hotplug_sock()
89{
90 int ret;
91
92 struct sockaddr_nl snl;
93 bzero(&snl, sizeof(struct sockaddr_nl));
94 snl.nl_family = AF_NETLINK;
95 snl.nl_pid = getpid();
96 snl.nl_groups = 1;
97
98 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
99 perror("signal");
100
101 int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
102 if (s == -1) {
103 perror("socket");
104 return -1;
105 }
106// setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
107
108 ret = bind(s, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
109 if (ret < 0) {
110 perror("bind");
111 close(s);
112 return -1;
113 }
114
115 return s;
116}
117
118int sd_app_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
119{
120 char sd_card_state[ZTE_HTTPSHARE_DEFAULT_LEN] = {0};
121 char cmd[MAX_CMD_LEN] = {0};
122
123 if (strncmp(msg, SD_ONLINE_MESSAGE, strlen(SD_ONLINE_MESSAGE)) == 0) {
124 memset(sd_card_state, 0, sizeof(sd_card_state));
125 cfg_get_item("sd_card_state", sd_card_state, sizeof(sd_card_state));
126
127 slog(SDCARD_PRINT, SLOG_DEBUG, "[%s][%d], state:%s\n", __func__, __LINE__, sd_card_state);
128
129 //0-°Î³ö£¬ 1-²åÈë
130 if (0 == strcmp("0", sd_card_state)) {
131 zte_httpshare_init();
132 slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]init sd mode!\n");
133 } else
134 slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]init sd ok!\n");
135 } else if (strncmp(msg, SD_OFFLINE_MESSAGE, strlen(SD_OFFLINE_MESSAGE)) == 0) {
136 slog(SDCARD_PRINT, SLOG_DEBUG, "[%s][%d]\n", __func__, __LINE__);
137 cfg_set("sd_card_state", "0");
138
139#if 0
140 zte_httpshare_call_system("umount /dev/mmcblk0");
141 zte_httpshare_call_system("umount /mnt/jffs2/etc_rw/config/mmc2");
142 zte_httpshare_call_system("umount /etc_rw/config/mmc2");
143#endif
144 zte_umount_dev();
145
146 memset(cmd, 0, sizeof(cmd));
147 snprintf(cmd, sizeof(cmd), "echo NULL > %s", usb_lun_path);
148 soft_system(cmd);
149 //zte_httpshare_call_system("echo NULL > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file");
150 slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]remove sd!\n");
151 }
152
153 return 0;
154}
155
156
157int sd_hotplug_init(viod)
158{
159 int cdrom = -1;
160
161 if (ZTE_HTTPSHARE_SUCCESS != zte_init_sdcard_path()) {
162 slog(SDCARD_PRINT, SLOG_ERR, "%s does not exist.\n", SD_CARD_PATH_PR);
163 return -1;
164 }
165
166 // »ñÈ¡usb lunÉ豸·¾¶
167 cdrom = sd_get_cdrom();
168 slog(SDCARD_PRINT, SLOG_ERR,"sd_hotplug:cdrom=%d\n",cdrom);
169 if (cdrom == 0)
170 memcpy(usb_lun_path, USB_HTTPSHARE_LUN0_PATH, strlen(USB_HTTPSHARE_LUN0_PATH));
171 else if(cdrom == 1)
172 memcpy(usb_lun_path, USB_HTTPSHARE_LUN1_PATH, strlen(USB_HTTPSHARE_LUN1_PATH));
173 else
174 {
175 slog(SDCARD_PRINT, SLOG_ERR, "lun for usb not exsited.\n");
176 return -1;
177 }
178 slog(SDCARD_PRINT, SLOG_ERR,"sd_hotplug usb_lun_path=%s\n", usb_lun_path);
179
180 hotplug_parse_register(DEVICE_TYPE_APP_SDCARD, sd_app_msg_parse);
181
182 return 0;
183}
184
185int sd_hotplug_main(int argc, char* argv[])
186{
187 int hotplug_sock = 0;
188 char sd_card_state[ZTE_HTTPSHARE_DEFAULT_LEN] = {0};
189 char buf[UEVENT_BUFFER_SIZE] = {0};
190 char cmd[MAX_CMD_LEN] = {0};
191 int len = 0;
192 int cdrom = -1;
193 prctl(PR_SET_NAME, "sd_hotplug", 0, 0, 0);
194
195 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
196 loglevel_init();
197
198 hotplug_sock = init_hotplug_sock();
199 if (hotplug_sock < 0) {
200 slog(SDCARD_PRINT, SLOG_ERR, "[sdcard_hotplug]create netlink socket failed!\n");
201 return -1;
202 }
203
204 if (ZTE_HTTPSHARE_SUCCESS != zte_init_sdcard_path()) {
205 slog(SDCARD_PRINT, SLOG_ERR, "%s does not exist.\n", SD_CARD_PATH_PR);
206 close(hotplug_sock);
207 return -1;
208 }
209
210 // »ñÈ¡usb lunÉ豸·¾¶
211 cdrom = sd_get_cdrom();
212 slog(SDCARD_PRINT, SLOG_ERR,"sd_hotplug:cdrom=%d\n",cdrom);
213 if(cdrom != 0 && cdrom != 1)
214 {
215 slog(SDCARD_PRINT, SLOG_ERR, "lun for usb not exsited.\n");
216 close(hotplug_sock);
217 return -1;
218 }
219
220 if (cdrom == 0){
221 len = strlen(USB_HTTPSHARE_LUN0_PATH);
222 memcpy(usb_lun_path, USB_HTTPSHARE_LUN0_PATH, len);
223 }else if(cdrom == 1){
224 len = strlen(USB_HTTPSHARE_LUN1_PATH);
225 memcpy(usb_lun_path, USB_HTTPSHARE_LUN1_PATH, len);
226 }
227
228 if(len >= PATH_LENGTH){
229 usb_lun_path[PATH_LENGTH -1] = '\0';
230 } else{
231 usb_lun_path[len] = '\0';
232 }
233
234 slog(SDCARD_PRINT, SLOG_ERR,"sd_hotplug usb_lun_path=%s\n", usb_lun_path);
235
236 while (1) {
237 /* Netlink message buffer */
238 memset(buf, 0, sizeof(buf));
239 len = recv(hotplug_sock, &buf, sizeof(buf)-1, 0);
240 if (len <= 0) {
241 continue;
242 }
243
244 if (strncmp(buf, SD_ONLINE_MESSAGE, strlen(SD_ONLINE_MESSAGE)) == 0) {
245 memset(sd_card_state, 0, sizeof(sd_card_state));
246 cfg_get_item("sd_card_state", sd_card_state, sizeof(sd_card_state));
247
248 slog(SDCARD_PRINT, SLOG_DEBUG, "[%s][%d], state:%s\n", __func__, __LINE__, sd_card_state);
249
250 //0-°Î³ö£¬ 1-²åÈë
251 if (0 == strcmp("0", sd_card_state)) {
252 zte_httpshare_init();
253 slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]init sd mode!\n");
254 } else
255 slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]init sd ok!\n");
256 } else if (strncmp(buf, SD_OFFLINE_MESSAGE, strlen(SD_OFFLINE_MESSAGE)) == 0) {
257 slog(SDCARD_PRINT, SLOG_DEBUG, "[%s][%d]\n", __func__, __LINE__);
258 cfg_set("sd_card_state", "0");
259
260#if 0
261 zte_httpshare_call_system("umount /dev/mmcblk0");
262 zte_httpshare_call_system("umount /mnt/jffs2/etc_rw/config/mmc2");
263 zte_httpshare_call_system("umount /etc_rw/config/mmc2");
264#endif
265 zte_umount_dev();
266
267 memset(cmd, 0, sizeof(cmd));
268 snprintf(cmd, sizeof(cmd), "echo NULL > %s", usb_lun_path);
269 soft_system(cmd);
270 //zte_httpshare_call_system("echo NULL > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file");
271 slog(SDCARD_PRINT, SLOG_NORMAL, "[sdcard-hotplug]remove sd!\n");
272 }
273 }
274
275 //close(hotplug_sock);//klocwork
276 return 0;
277}