📄 appli.c
字号:
/********************************************************************************************//* Appli.c - Copyright Wavecom S.A. (c) 2006 *//* *//* *//* DISCLAIMER OF WARRANTY *//* ====================== *//* This Software is provided free of charge on an 'as is' basis. No warranty is given *//* by Wavecom S.A. in relation to the Software of the uses to which it may be put by you, *//* the user, or its merchantability, fitness or suitability for any particular purpose *//* or conditions; and/or that the use of the Software and all documentation relating *//* thereto by the Licensee will not infringe any third party copyright or other *//* intellectual property rights. Wavecom S.A. shall furthermore be under no obligation *//* to provide support of any nature for the Software and the Documentation. *//* *//* LIMIT OF LIABILITY *//* ================== *//* In no event shall Wavecom S.A. be liable for any loss or damages whatsoever or howsoever *//* caused arising directly or indirectly in connection with this licence, the Software, *//* its use or otherwise except to the extent that such liability may not be lawfully *//* excluded. Notwithstanding the generality of the foregoing, Wavecom S.A. expressly *//* excludes liability for indirect, special, incidental or consequential loss or damage *//* which may arise in respect of the Software or its use, or in respect of other equipment *//* or property, or for loss of profit, business, revenue, goodwill or anticipated savings. *//* *//********************************************************************************************//***************************************************************************//* File : Appli.c *//*-------------------------------------------------------------------------*//* Object : Customer application *//* *//* contents : Customer main procedures *//* *//* Change : *//***************************************************************************//* * -------------------------------------------------------------------------- * Date | Author | Revision | Description * ----------+--------+----------------+------------------------------------- * 22.05.06 | FFT | 1.0 | Initial revision. * ----------+--------+----------------+------------------------------------- * 24.07.06 | RFN | | Increased wm_apmCustomStackSize * | | | for compiling with ARM * ----------+--------+----------------+-------------------------------------*//* define either OVER_GPRS for GPRS connection, or OVER_PPP for PPP server * over PPP_BEARER. Don't define both simultaneously! */#define OVER_PPP//#define OVER_GPRS#if defined( OVER_PPP) && defined( OVER_GPRS)#error "Choose EITHER OVER_PPP OR OVER_GPRS, not both!"#endif#ifdef OVER_PPP#define PPP_BEARER "UART2"#define PPP_PASSWORD "123456"#define PPP_LOCAL_STRADDR "192.168.1.4" /* Module's address */#define PPP_DEST_STRADDR "192.168.1.5" /* PPP client's address *//* An UDP packet with the module's IP address will be sent * to this host (PPP server case). */#define HELLO_STRADDR PPP_DEST_STRADDR#define HELLO_PORT 2020#endif#ifdef OVER_GPRS/* Update these settings according to your GPRS provider. */#define GPRS_APN "fipbouygtel.com"#define GPRS_LOGIN "a2b"#define GPRS_PASSWORD "access"#define GPRS_PINCODE "3699"/* An UDP packet with the module's IP address will be sent * to this host (GRPS case). */#define HELLO_STRADDR "somehost.example.com"#define HELLO_PORT 2020#endif#include "adl_global.h"#include "wip.h"/* Echo-everything-uppercased service port. */#define ECHO_PORT 1234/* Log watching service port. */#define LOG_PORT 2000#define MSG_WELCOME_LOG "Hi, you're looking at wipsample's logs.\n" #define MSG_WELCOME_ECHO "Hi, I'm a shouter: I'll repeat "\ "everything you type UPPERCASED!\n"#define MSG_BYE_APPLI "Shutting down everything and quitting. Bye!"#define ASSERT( pred) \if( !(pred)) tcplog( "ASSERTION FAILURE: " #pred "\n")#define ASSERT_OK( v) ASSERT( 0 == (v))/***************************************************************************//* Global variables *//*-------------------------------------------------------------------------*/static wip_in_addr_t my_address = 0;/***************************************************************************//* Mandatory variables *//*-------------------------------------------------------------------------*//* wm_apmCustomStackSize *//*-------------------------------------------------------------------------*//***************************************************************************/#if __OAT_API_VERSION__ >= 400const u16 wm_apmCustomStackSize = 4096;#elseu32 wm_apmCustomStack[1024];const u16 wm_apmCustomStackSize = sizeof(wm_apmCustomStack);#endif/***************************************************************************//* Logging system *//* -------------- *//* This system allow multiple TCP client to subscribe to logging informa- *//* tion: every string passed through tcplog(...) will be received by them.*//* In addtion, these strings are also reported to the wavecom TRACE *//* facility. *//* *//* Internal details: *//* - channels to the clients are stored in the table log_readers. *//* - the neumber of current clients is in log_readers_nb. *//* - there can be up to LOG_READERS_MAX clients simultaneously. *//* - the server channel is kept in log_channel. *//* *//***************************************************************************/#define LOG_READERS_MAX 16static wip_channel_t log_channel;static wip_channel_t log_readers[ LOG_READERS_MAX];static int log_readers_nb = 0;/* Send a message to all registered log readers. */static void tcplog( ascii *text) { int i, textlen; TRACE (( 1, text )); for( textlen = 0; text [textlen]; textlen++); for( i = 0; i < log_readers_nb; i++) wip_write( log_readers [i], text, textlen);}/* React to open / close events on log reader channels. */static void log_evh( wip_event_t *ev, void *ctx){ int i; switch( ev->kind) { case WIP_CEV_OPEN: /* Register the newly open reader in the table. */ if( LOG_READERS_MAX == log_readers_nb) { tcplog( "Too many log readers\n"); wip_close( ev->channel); return; } else { log_readers [log_readers_nb++] = ev->channel; wip_write( ev->channel, MSG_WELCOME_LOG, sizeof( MSG_WELCOME_LOG) - 1); tcplog( "New log watcher added."); } break; case WIP_CEV_PEER_CLOSE: /* Suppress the channel from the list. */ for( i = 0; i < log_readers_nb && log_readers [i] != ev->channel; i++); if( log_readers_nb == i) return; /* Weird: the channel wasn't registered?! */ for( ; i < log_readers_nb; i++) log_readers [i] = log_readers [i + 1]; log_readers_nb--; tcplog( "Log watcher closed\n"); wip_close( ev->channel); break; }}static void tcplogInit() { log_channel = wip_TCPServerCreate( LOG_PORT, log_evh, NULL); log_readers_nb = 0;}static void tcplogClose(){ int i; tcplog( MSG_BYE_APPLI); for( i = 0; i < log_readers_nb; i++) wip_close( log_readers [i]); log_readers_nb = 0;}/***************************************************************************//* echo service handling *//***************************************************************************//* Repeats on the channel's output whatever comes on the channel's input. * Changes lowercase chars into uppercase ones. */static void echo_data( wip_channel_t c){ char buffer [256]; int i, nread, nwrite; do { nread = wip_read( c, buffer, sizeof( buffer)); if( nread < 0) { tcplog( "read error\n"); return; } tcplog( "."); /* Turn lowercase letters into uppercase ones. */ for( i = 0; i < nread; i++) if( buffer [i] >= 'a' && buffer [i] <= 'z') buffer [i] += 'A' - 'a'; nwrite = wip_write( c, buffer, nread); if( nwrite < nread) tcplog( "can't write as much as I wish!?\n"); } while( sizeof( buffer) == nread);}/* Dispatch events on echo channels. */static void evh_echo( wip_event_t *ev, void *ctx) { switch( ev->kind) { case WIP_CEV_OPEN: tcplog( MSG_WELCOME_ECHO "\n"); wip_write( ev->channel, MSG_WELCOME_ECHO, sizeof( MSG_WELCOME_ECHO) - 1); break; case WIP_CEV_PEER_CLOSE: tcplog( "Echo service closed.\n"); wip_close( ev->channel); break; case WIP_CEV_READ: echo_data( ev->channel); break; }}/* Send my address, in numeric ascii form, to HELLO_STRADDR:HELLO_PORT. */void evh_hello( wip_event_t *ev, void *ctx) { if( WIP_CEV_OPEN == ev->kind) { bool r; ascii text [16]; r = wip_inet_ntoa( my_address, text, sizeof(text)); ASSERT( r); wip_write( ev->channel, text, strlen( text)); wip_close( ev->channel); }}/***************************************************************************//* Initialization-related event handlers *//***************************************************************************/#ifdef OVER_PPP/* ppp authentication events: in PPP server mode, when a client * authenticates itself, fill the password so that it can be chaecked. */static s8 evh_ppp_auth( wip_bearer_t b, wip_bearerServerEvent_t *ev, void *ctx) { if( WIP_BEV_PPP_AUTH_PEER != ev->kind) return 1; /* I don't check user Id, although I should... The result is: * PPP client can give any UID, as long as the password is PPP_PASSWORD. * To refuse a UID, return 0. */ ev->content.ppp_auth.secret = PPP_PASSWORD; ev->content.ppp_auth.secretlen = sizeof( PPP_PASSWORD) - 1; return 1; return 0;}#endif /* OVER_PPP *//* bearer events handler: when the bearer connection is completed, * start IP services (used both in PPP and GPRS modes). */static void evh_bearer( wip_bearer_t b, s8 event, void *ctx){ wip_channel_t echo_server, c; if( WIP_BEV_IP_CONNECTED != event) return; tcplogInit(); echo_server = wip_TCPServerCreate( ECHO_PORT, evh_echo, NULL); ASSERT( echo_server); /* Put my inet address in the global var my_address */ wip_bearerGetOpts( b, WIP_BOPT_IP_ADDR, &my_address, WIP_BOPT_END); wip_debug( "\nMy address: %I.\n\n", my_address); /* Send it through UDP to a specified host. Actual sending will be performed * by evh_hello once the connection is established. */ c = wip_UDPCreateOpts( evh_hello, NULL, WIP_COPT_PEER_STRADDR, HELLO_STRADDR, WIP_COPT_PEER_PORT, HELLO_PORT, WIP_COPT_END); ASSERT( c);}#ifdef OVER_GPRS/* sim events: in GPRS mode, when SIM initialisation is completed, * open the GPRS bearer. */static void evh_sim( u8 event) { int r; wip_bearer_t b; if( ADL_SIM_EVENT_FULL_INIT != event) return; r = wip_bearerOpen( &b, "GPRS", evh_bearer, NULL); ASSERT_OK( r); r = wip_bearerSetOpts( b, WIP_BOPT_GPRS_APN, GPRS_APN, WIP_BOPT_LOGIN, GPRS_LOGIN, WIP_BOPT_PASSWORD, GPRS_PASSWORD, WIP_BOPT_END); ASSERT_OK( r); r = wip_bearerStart( b); ASSERT_OK( 0 == r || WIP_BERR_OK_INPROGRESS == r);}#endif /* OVER_GPRS *//***************************************************************************//* Function : adl_main *//*-------------------------------------------------------------------------*//* Object : Customer application initialisation *//* *//*-------------------------------------------------------------------------*//* Variable Name |IN |OUT|GLB| Utilisation *//*--------------------+---+---+---+----------------------------------------*//* InitType | | | | Application start mode reason *//*--------------------+---+---+---+----------------------------------------*//***************************************************************************/void adl_main ( adl_InitType_e InitType ){ int r; TRACE (( 1, "Embedded Application : Main" )); r = wip_netInit(); ASSERT_OK( r);#ifdef OVER_PPP { wip_in_addr_t local, dest; wip_bearer_t b; wip_inet_aton( "192.168.1.4", &local); wip_inet_aton( "192.168.1.5", &dest); r = wip_bearerOpen( &b, PPP_BEARER, evh_bearer, NULL); ASSERT_OK( r); r = wip_bearerSetOpts( b, WIP_BOPT_IP_ADDR, local, WIP_BOPT_IP_DST_ADDR, dest, WIP_BOPT_IP_SETDNS, FALSE, WIP_BOPT_IP_SETGW, FALSE, WIP_BOPT_RESTART, FALSE, WIP_BOPT_END); ASSERT_OK( r); r = wip_bearerStartServer( b, evh_ppp_auth, NULL); ASSERT_OK( r); }#elif defined( OVER_GPRS) /* Connection will be made after the SIM card is fully * initialized. */ adl_simSubscribe( evh_sim, GPRS_PINCODE);#else#error "Define either OVER_GPRS or OVER_PPP!"#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -