1+
2+ #include "../new_common.h"
3+ #include "../new_pins.h"
4+ #include "../new_cfg.h"
5+ // Commands register, execution API and cmd tokenizer
6+ #include "../cmnds/cmd_public.h"
7+ #include "../cmnds/cmd_local.h"
8+ #include "../logging/logging.h"
9+
10+ #if PLATFORM_BK7252 || PLATFORM_BK7252N
11+
12+ #include "net.h"
13+ #include "typedef.h"
14+ #include "arm_arch.h"
15+ #include "uart_pub.h"
16+ #include "rtos_pub.h"
17+ #include "error.h"
18+ #include "video_transfer.h"
19+ #include "i2c_pub.h"
20+ #include "jpeg_encoder_pub.h"
21+
22+ #define MJPEG_BOUNDARY "boundarydonotcross"
23+ #define MAX_BUF_SIZE 40*1024
24+ static int g_mjpeg_stop = 0 ;
25+ static char g_send_buf [1024 ];
26+ static beken_thread_t thread ;
27+
28+ int send_first_response (int client )
29+ {
30+ g_send_buf [0 ] = 0 ;
31+
32+ snprintf (g_send_buf , 1024 ,
33+ "HTTP/1.0 200 OK\r\n"
34+ "Connection: close\r\n"
35+ "Server: MJPG-Streamer/0.2\r\n"
36+ "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0,"
37+ " post-check=0, max-age=0\r\n"
38+ "Pragma: no-cache\r\n"
39+ "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
40+ "Content-Type: multipart/x-mixed-replace;boundary="
41+ MJPEG_BOUNDARY "\r\n"
42+ "\r\n"
43+ "--" MJPEG_BOUNDARY "\r\n" );
44+ if (send (client , g_send_buf , strlen (g_send_buf ), 0 ) < 0 )
45+ {
46+ close (client );
47+ return -1 ;
48+ }
49+
50+ return 0 ;
51+ }
52+
53+ int mjpeg_send_stream (int client , void * data , int size )
54+ {
55+ g_send_buf [0 ] = 0 ;
56+
57+ if (!g_mjpeg_stop )
58+ {
59+ snprintf (g_send_buf , 1024 ,
60+ "Content-Type: image/jpeg\r\n"
61+ "Content-Length: %d\r\n"
62+ "\r\n" , size );
63+ if (send (client , g_send_buf , strlen (g_send_buf ), 0 ) < 0 )
64+ {
65+ close (client );
66+ return -1 ;
67+ }
68+
69+ if (send (client , data , size , 0 ) < 0 )
70+ {
71+ close (client );
72+ return -1 ;
73+ }
74+
75+ g_send_buf [0 ] = 0 ;
76+ snprintf (g_send_buf , 1024 , "\r\n--" MJPEG_BOUNDARY "\r\n" );
77+ if (send (client , g_send_buf , strlen (g_send_buf ), 0 ) < 0 )
78+ {
79+ close (client );
80+ return -1 ;
81+ }
82+
83+ return 0 ;
84+ }
85+
86+ return -1 ;
87+ }
88+
89+ void mjpeg_server_thread (void * arg )
90+ {
91+ int on ;
92+ int srv_sock = -1 ;
93+ int fream_length = 0 ;
94+ struct sockaddr_in addr ;
95+ socklen_t sock_len = sizeof (struct sockaddr_in );
96+
97+ //int bufsz = 50 * 1024;
98+ uint8_t * buf = (uint8_t * )malloc (MAX_BUF_SIZE );
99+
100+ if (!buf )
101+ {
102+ bk_printf ("no buffer yet!\n" );
103+ return ;
104+ }
105+
106+
107+ srv_sock = socket (AF_INET , SOCK_STREAM , 0 );
108+ if (srv_sock < 0 )
109+ {
110+ bk_printf ("mjpeg_server: create server socket failed due to (%s)\n" ,
111+ strerror (errno ));
112+ goto exit ;
113+ }
114+
115+ bzero (& addr , sock_len );
116+ addr .sin_family = AF_INET ;
117+ addr .sin_port = htons (7070 );
118+ addr .sin_addr .s_addr = INADDR_ANY ;
119+
120+ /* ignore "socket already in use" errors */
121+ on = 1 ;
122+ lwip_setsockopt (srv_sock , SOL_SOCKET , SO_REUSEADDR , & on , sizeof (on ));
123+ lwip_setsockopt (srv_sock , IPPROTO_TCP , TCP_NODELAY , & on , sizeof (on ));
124+
125+ if (bind (srv_sock , (struct sockaddr * )& addr , sock_len ) != 0 )
126+ {
127+ bk_printf ("mjpeg_server: bind() failed due to (%s)\n" ,
128+ strerror (errno ));
129+ goto exit ;
130+ }
131+
132+ if (listen (srv_sock , 2 ) != 0 )
133+ {
134+ bk_printf ("mjpeg_server: listen() failed due to (%s)\n" ,
135+ strerror (errno ));
136+ goto exit ;
137+ }
138+
139+ g_mjpeg_stop = 0 ;
140+ while (!g_mjpeg_stop )
141+ {
142+ struct sockaddr_in client_addr ;
143+ int client = accept (srv_sock , (struct sockaddr * )& client_addr , & sock_len );
144+ if (client < 0 )
145+ continue ;
146+
147+ bk_printf ("mjpeg_server: client connected\n" );
148+ if (send_first_response (client ) < 0 )
149+ {
150+ client = -1 ;
151+ continue ;
152+ }
153+
154+ while (1 )
155+ {
156+ fream_length = 0 ;
157+ /* capture a jpeg frame */
158+ fream_length = video_buffer_read_frame (buf , MAX_BUF_SIZE , NULL , 1000 );
159+ bk_printf ("len:%d\r\n" ,fream_length );
160+ if (fream_length != 0 )
161+ {
162+ /* send out this frame */
163+ if (mjpeg_send_stream (client , (void * )buf , fream_length ) < 0 )
164+ {
165+ bk_printf ("client disconnected!\n" );
166+ break ;
167+ }
168+ }
169+ if (g_mjpeg_stop ) break ;
170+ taskYIELD ();
171+ }
172+ taskYIELD ();
173+ }
174+
175+ exit :
176+ if (srv_sock >= 0 )
177+ close (srv_sock );
178+ if (buf )
179+ {
180+ free (buf );
181+ buf = NULL ;
182+ }
183+ }
184+
185+ void scan_camera_sensors ()
186+ {
187+
188+ DJPEG_DESC_ST ejpeg_cfg ;
189+
190+ UINT32 status ;
191+
192+ DD_HANDLE ejpeg_hdl = ddev_open (EJPEG_DEV_NAME , & status , (UINT32 )& ejpeg_cfg );
193+
194+ bk_printf ("open EJPEG %p\r\n" , ejpeg_hdl );
195+ bk_printf ("status: %d\r\n" , status );
196+
197+ UINT32 i2c2_trans_mode = (0 & (~I2C2_MSG_WORK_MODE_MS_BIT ) // master
198+ & (~I2C2_MSG_WORK_MODE_AL_BIT )) // 7bit address
199+ | (I2C2_MSG_WORK_MODE_IA_BIT );
200+
201+ //UINT32 oflag = 0;
202+
203+ DD_HANDLE i2c_hdl = ddev_open (I2C2_DEV_NAME , & status , i2c2_trans_mode );
204+ bk_printf ("open I2C2\r\n" );
205+ bk_printf ("status: %d\r\n" , status );
206+
207+ unsigned char data ;
208+ I2C_OP_ST i2c_operater ;
209+
210+
211+ //status = ddev_write(i2c_hdl, (char *)&data, 1, (UINT32)&i2c_operater);
212+
213+
214+ //i2c_operater.addr_width = ADDR_WIDTH_8;
215+
216+
217+ for (int i = 0 ; i < 128 ; i ++ )
218+ {
219+
220+ i2c_operater .salve_id = (i << 1 ) | 1 ;
221+
222+ i2c_operater .op_addr = 0x0F0 ;
223+
224+ data = 0x00 ;
225+
226+ //status = ddev_write(i2c_hdl, (char *) data, 1, (UINT32)&i2c_operater);
227+
228+ //os_printf("\nREAD -> Status: %d, Data: %d %d %d %d", status, data[0], data[1], data[2], data[3]);
229+
230+ status = ddev_read (i2c_hdl , (char * )& data , 1 , (UINT32 )& i2c_operater );
231+
232+ bk_printf ("READ -> Status: %d, Device: %x, Addr: %x, Data: %x \n\n" , status , i2c_operater .salve_id , i2c_operater .op_addr , data );
233+
234+ }
235+
236+
237+ }
238+
239+ void BK_Cam_Init (void )
240+ {
241+ scan_camera_sensors ();
242+ video_buffer_open ();
243+ rtos_create_thread (thread , 20 , "jpeg_stream" , mjpeg_server_thread , 2048 , NULL );
244+ }
245+
246+ void BK_Cam_Deinit (void )
247+ {
248+ g_mjpeg_stop = 0 ;
249+ rtos_delete_thread (thread );
250+ video_transfer_deinit ();
251+ }
252+
253+ void BK_Cam_RunEverySecond (void )
254+ {
255+
256+ }
257+
258+ #endif
0 commit comments