1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 #if AOS_COMP_CLI
6 #include <string.h>
7 #include <stdbool.h>
8 #include "lwip/opt.h"
9 #include "lwip/debug.h"
10 #include "lwip/err.h"
11 #include <aos/cli.h>
12 #include "aos/kernel.h"
13 #include "uservice/uservice.h"
14 #include "uservice/eventid.h"
15 
16 /******************************************************
17  *                      Macros
18  ******************************************************/
19 /******************************************************
20  *                    Constants
21  ******************************************************/
22 
23 /******************************************************
24  *                   Enumerations
25  ******************************************************/
26 
27 /******************************************************
28  *                 Type Definitions
29  ******************************************************/
30 
31 /******************************************************
32  *                    Structures
33  ******************************************************/
34 void sendfile_command( char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv );
35 extern int sendfile_client(int argc,char *argv[]);
36 extern int sendfile_server(int argc, char **argv);
37 
38 struct cli_command sendfile_message_cmd[] = {
39     { "sendfile", "sendfile client/server app", sendfile_command },
40 };
41 
42 /******************************************************
43  *               Function Declarations
44  ******************************************************/
45 void sendfile_help( int argc, char **argv );
46 
47 /******************************************************
48  *               Variables Definitions
49  ******************************************************/
50 
51 /******************************************************
52  *               Function Definitions
53  ******************************************************/
54 
sendfile_help(int argc,char ** argv)55 void sendfile_help( int argc, char **argv )
56 {
57     LWIP_DEBUGF( SENDFILE_DEBUG, ("Usage: sendfile -c/-s [Options]\n"));
58     LWIP_DEBUGF( SENDFILE_DEBUG, ("       sendfile  [-h]\n"));
59     LWIP_DEBUGF( SENDFILE_DEBUG, ("Server specific:\n"));
60     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -s,        sendfile server\n"));
61     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -d,        destination addr\n"));
62     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -p,        destination port\n"));
63     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -f,        to download file from server\n"));
64     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -n,        to save file\n"));
65     LWIP_DEBUGF( SENDFILE_DEBUG, ("Client specific:\n"));
66     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -c,        sendfile client\n"));
67     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -p,        source listening port\n"));
68     LWIP_DEBUGF( SENDFILE_DEBUG, ("Miscellaneous:\n" ));
69     LWIP_DEBUGF( SENDFILE_DEBUG, ("  -h,        Show help\n"));
70     LWIP_DEBUGF( SENDFILE_DEBUG, ("Example:\n"));
71     LWIP_DEBUGF( SENDFILE_DEBUG, ("sendfile -c -d 192.168.0.1 -p 12345 -f 1.txt -n 2.txt\n"));
72     LWIP_DEBUGF( SENDFILE_DEBUG, ("sendfile -s -p 12345\n"));
73 }
74 
75 static bool      m_got_ip = false;
76 
sendfile_command(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)77 void sendfile_command( char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv )
78 {
79     if ( m_got_ip == false ) {
80         LWIP_DEBUGF( SENDFILE_DEBUG, ("Connect network first!\n"));
81         return;
82     }
83 
84     if ( argc < 2 ) {
85          LWIP_DEBUGF( SENDFILE_DEBUG, ("Invalid command\n"));
86          sendfile_help( 0, NULL );
87         return;
88     }
89 
90     if ( strcmp( argv[1], "-c" ) == 0 ) {
91         sendfile_client( argc - 1, &argv[1] );
92     }
93     else if ( strcmp( argv[1], "-s" ) == 0 ) {
94         sendfile_server( argc - 1, &argv[1] );
95     }
96     else {
97         sendfile_help( argc - 1, &argv[1] );
98     }
99 }
100 
wifi_event_cb(uint32_t event_id,const void * param,void * context)101 static void wifi_event_cb(uint32_t event_id, const void *param, void *context)
102 {
103     if (event_id != EVENT_NETMGR_DHCP_SUCCESS) {
104         return;
105     }
106 
107     m_got_ip = true;
108 }
109 
sendfile_cli_register(void)110 int sendfile_cli_register( void )
111 {
112     event_subscribe(EVENT_NETMGR_DHCP_SUCCESS, wifi_event_cb, NULL);
113 
114     if( 0 == aos_cli_register_commands( sendfile_message_cmd, 1 ) ) {
115         return ERR_OK;
116     }
117     else {
118         return ERR_VAL;
119     }
120 }
121 #endif /* AOS_COMP_CLI */
122