1、Network Service,Yao.wen,2018/4/12,Copyright MediaTek Inc. All rights reserved.,1,Agenda,CBM OverviewCBM OverviewCBM APIsSample CodeSocket OverviewSocket OverviewSocket APIsSample CodeWPS OverviewWPS OverviewWPS MessagesSample Code,CBM,2018/4/12,Copyright MediaTek Inc. All rights reserved.,3,CBM Over
2、view,CBM provides a set of APIs that all internet applications could register / deregister app id and encode / decode app_id, sim info from/into Data account ID. These APIs are thread-safed, they could be called by any task.,2018/4/12,Copyright MediaTek Inc. All rights reserved.,4,Common Data Accoun
3、t Selecting Screen,mmi_dtcnt_select_account_option_with_id() will launch this screen,When user select an account, application will get the selected account id from callback function,2018/4/12,Copyright MediaTek Inc. All rights reserved.,5,CBM APIs,2018/4/12,Copyright MediaTek Inc. All rights reserve
4、d.,6,CBM APIs,2018/4/12,Copyright MediaTek Inc. All rights reserved.,7,Coding Guide,Register app_idg_app_id = cbm_register_app_id(APP_STR_ID, APP_ICON_ID); /* Register app_id */Launch Data Account Screenmmi_dtcnt_select_account_option_with_id(data_account_callback, /* Callback Functions */MENU_ID_AP
5、P, /* Menu ID */DATA_ACCOUNT_BEARER_GPRS | DATA_ACCOUNT_BEARER_WIFI, /* Bearer */0, /* DTCNT_SELECT_SIM: Dual SIM supported, 0 : dont support dual SIM*/g_app_id, /* app_id from step 1 */0); /* Default selected account in MMI screen */data_account_callback (U32 index) /* index user selected */mmi_dtc
6、nt_get_account_name(index, buf, len); /* Account Name for Display */g_data_account_id = index;Create Socket and Make Connectionsoc_create(., g_data_account_id); /* Create socket and make connections */,Socket,2018/4/12,Copyright MediaTek Inc. All rights reserved.,9,The Socket module provides a Socke
7、t API for applications, e.g., WAP, Email, etc. The Socket API consists of several function calls. Each function is implemented in the way of thread-safe and reentrant. Socket API is largely based on Berkeley Socket API.,Socket Overview,2018/4/12,Copyright MediaTek Inc. All rights reserved.,10,Socket
8、 Overview,LayeringSocket Applications (e.g., WAP, Email)Socket Layer (SOC task)Protocol Layer (TCPIP task: TCP, UDP, IP, ICMP, ARP)Bearer Layer (PPP, TCM, WNDRV)CBM (Central Bearer Management),WAP,SOC,TCPIP,PPP,TCM,WNDRV,Email,CBM,2018/4/12,Copyright MediaTek Inc. All rights reserved.,11,Socket Mode
9、,Socket blocking (default) modeSocket non-blocking mode Non-blocking + Asynchronous notification mode,2018/4/12,Copyright MediaTek Inc. All rights reserved.,12,Blocking mode,2018/4/12,Copyright MediaTek Inc. All rights reserved.,13,Non-blocking mode,2018/4/12,Copyright MediaTek Inc. All rights reser
10、ved.,14,Asynchronous notification mode,2018/4/12,Copyright MediaTek Inc. All rights reserved.,15,SOC APP SAP,SOC,APP,APP_SOC_GET_HOST_BY_NAME_INDAPP_SOC_GET_HOST_BY_ADDR_INDAPP_SOC_NOTIFY_IND,soc_create, soc_bind,soc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_getsockopt, soc_setsockoptsoc_
11、gethostbyname, soc_gethostbyname2soc_gethostbyaddrsoc_get_account_localipsoc_getsockaddrsoc_get_last_errorsoc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,16,Message,APP_SOC_GET_HOST_BY_NAME_IND APP_SOC_GET_HOST_BY_ADDR_IND APP_SOC_NOTIFY_IND,2018/4/12,Copyright MediaTek Inc. All rig
12、hts reserved.,17,Primitives,APP_SOC_GET_HOST_BY_NAME_INDResponse of soc_gethostbyname()Fields:addr : returned IP addressaddr_len: returned IP address lengthaccess_id: set in soc_gethostbyname(),APP_SOC_GET_HOST_BY_NAME_INDAPP_SOC_GET_HOST_BY_ADDR_INDAPP_SOC_NOTIFY_IND,2018/4/12,Copyright MediaTek In
13、c. All rights reserved.,18,Primitives,APP_SOC_GET_HOST_BY_ADDR_INDResponse of soc_gethostbyaddr()Fields:name: returned domain name (null terminated)access_id: set in soc_gethostbyaddr(),APP_SOC_GET_HOST_BY_NAME_INDAPP_SOC_GET_HOST_BY_ADDR_INDAPP_SOC_NOTIFY_IND,2018/4/12,Copyright MediaTek Inc. All r
14、ights reserved.,19,Primitives,APP_SOC_NOTIFY_INDUsed to give notifications of network eventsFieldsevent_type: SOC_READNotify to read SOC_WRITENotify to write SOC_ACCEPTNotify to accept SOC_CONNECT Notify to connect SOC_CLOSENotify to closeresult: Indicate CONNECT succeed or noterror_cause,APP_SOC_GE
15、T_HOST_BY_NAME_INDAPP_SOC_GET_HOST_BY_ADDR_INDAPP_SOC_NOTIFY_IND,2018/4/12,Copyright MediaTek Inc. All rights reserved.,20,Functions,soc_createTo create a socketFields:domain:Currently, only PF_INET supported type: SOCK_STREAM/SOCK_DGRAMprotocol:Shall set to zeromod_id:MOD_GISnwk_account_id:Network
16、account idReturn:=0socket_id0errorInfo:Blocking,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reser
17、ved.,21,Functions,soc_bindTo bind a local ip address to a socketFields:s: socket_idaddr:local ip address, portReturn:0:success0number of bytes sentSOC_WOULDBLOCK in progressothersfail,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbyn
18、amesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,24,Functions,soc_recv, soc_recvfromTo receive data (and return the source address)Fields:s: socket_idbuf:buffer pointer to datalen:length of buffer*fromaddr:ip
19、 address, port (only for soc_recvfrom)Returns:0number of bytes receivedSOC_WOULDBLOCK in progressothersfailInfo:Need to call iteratively till returns SOC_WOULDBLOCK,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbynam
20、e2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,25,Functions,soc_setsockopt/soc_getsockoptTo set (get) options on sockets.Fields:s: socket_idoption:option_type (soc_option_enum)val:option valuevalsize:size of the option valueR
21、eturns:0success0fail,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,26,Socket Options,SOC_
22、NBIOSet to non-blocking modeSOC_ASYNCSpecify the events application wants to receive.SOC_READ/SOC_WRITE/SOC_CONNECT/SOC_CLOSESOC_UDP_ANY_FPORTFor a connected UDP socket, those packets from the same remote address but different port will also be accepted.,soc_create, soc_bindsoc_connectsoc_send, soc_
23、sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_close_nwk_accountsoc_close_nwk_account_by_idsoc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,27,Functions,soc_gethostbynameTo get ip
24、address by giving domain nameFields:is_blocking only non-blocking supportedmod_idMOD_GISrequest_idto distinguish different DNS requestdomain_name*addr/ *addr_lenreturned ip address and lengthaccess_idembeded in GET_HOST_BY_NAME_INDnwk_account_idnetwork account idReturns:0successSOC_WOULDBLOCK In pro
25、gressothersfail,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,28,Functions,soc_gethostbyn
26、ame2To get ip addresses by giving domain nameFields:is_blocking only non-blocking supported*addr/*addr_lenreturned ip addresses, lengthin_entry_numMax allowed number of return entries*out_entry_numNumber of returned entriesReturns:0successSOC_WOULDBLOCK in progressothersfail,soc_create, soc_bindsoc_
27、connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,29,Functions,soc_get_last_errorTo get the last network error.Fields
28、:s: socket_iderror:SOC_BEARER_FAIL/othersdeatil_cause:detailed errorReturns:0success0fail,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/1
29、2,Copyright MediaTek Inc. All rights reserved.,30,Functions,soc_get_account_localipTo get the local IP address of the socketFields:s: socket_id*local_ip:returned ip addressReturnes:0success0fail,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc
30、_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,31,Functions,soc_ip_checkTo check if the input string in the valid form of IP address.Fields:asci_addr:domain name or IP address*ip_addr:returned IP
31、 address (in byte format)*ip_validity the result of checkReturns:KAL_TRUE an IP addressKAL_FALSEnot an IP address,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethostbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_c
32、heck, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,32,Functions,soc_closeTo close a socketFieldss: socket_idReturns:0success0failInfo:close the socket,soc_create, soc_bindsoc_connectsoc_send, soc_sendtosoc_recv, soc_recvfromsoc_setsockopt/soc_getsockoptsoc_gethostbynamesoc_gethos
33、tbyname2soc_get_last_errorsoc_get_account_localipsoc_ip_check, soc_close,2018/4/12,Copyright MediaTek Inc. All rights reserved.,33,Sample code,Socket Usage Sample Codes (Client side):,/* create socket */handle = soc_create(SOC_PF_INET, SOC_SOCK_STREAM, 0, MOD_JAYSN, nwk_acct_id);if (handle 0) return
34、 -1;/* set socket option */option = SOC_READ | SOC_WRITE | SOC_ACCEPT | SOC_CONNECT | SOC_CLOSE;result = soc_setsockopt(handle, SOC_ASYNC, if (result = SOC_WOULDBLOCK) /* wait for the APP_SOC_NOTIFY_IND(SOC_CONNECT) */,2018/4/12,Copyright MediaTek Inc. All rights reserved.,34,Sample code,Socket Usag
35、e Sample Codes (Server side):,sockaddr_struct addr;Kal_mem_set(,WPS,2018/4/12,Copyright MediaTek Inc. All rights reserved.,36,WPS Overview,WAP 2.0 Protocol Service (WPS) provides interface for application that needs to communicate with WAP gateway, HTTP proxy/server through WAP 2.0 protocol stackCha
36、nnel Setup:Configure a specific channel to handle HTTP/WSP requestHTTP and WSP request:Send HTTP/WSP request for applicationReceive HTTP/WSP response for applicationCache and cookie clear request:Clear cache or cookie, which is shared by all users,2018/4/12,Copyright MediaTek Inc. All rights reserve
37、d.,37,WPS Overview,Application,Socket Task,Function call,soc_create, soc_close, soc_connect,soc_send, soc_sendto,soc_recv, soc_recvfrom,APP_SOC_GET_HOST_BY_NAME_INDAPP_SOC_GET_HOST_BY_ADDR_INDAPP_SOC_NOTIFY_IND,MSG_ID_WPS_SET_CHANNEL_REQMSG_ID_WPS_HTTP_REQMSG_ID_WPS_READ_CONTENT_REQ,MSG_ID_WPS_SET_C
38、HANNEL_RSPMSG_ID_WPS_HTTP_RSPMSG_ID_WPS_READ_CONTENT _RSP,2018/4/12,Copyright MediaTek Inc. All rights reserved.,38,WPS Message,2018/4/12,Copyright MediaTek Inc. All rights reserved.,39,WPS Message,2018/4/12,Copyright MediaTek Inc. All rights reserved.,40,WPS Message,2018/4/12,Copyright MediaTek Inc
39、. All rights reserved.,41,MSG_ID_WPS_SET_CHANNEL_REQLocal Parameter wps_set_channel_req_struct,2018/4/12,Copyright MediaTek Inc. All rights reserved.,42,MSG_ID_WPS_SET_CHANNEL_REQ Peer buffer wps_set_channel_req_struct,If application doesnt specify Static_header, then WPS will use the value currentl
40、y used by WAPStatic_header (if necessary) must be filled in wps_set_channel_req_var_struct and then packed in peer buffer by wps_pun_var_part() before sending to WPS. Static_header will be used in the header when establishing WSP session when conn_type is WSP_CONN_TYPE_WSP_CO and the first WSP reque
41、st of some dedicated channel with connection type WSP_CONN_TYPE_WSP_CL.,2018/4/12,Copyright MediaTek Inc. All rights reserved.,43,MSG_ID_WPS_HTTP_REQ Local Parameter wps_http_req_struct,2018/4/12,Copyright MediaTek Inc. All rights reserved.,44,MSG_ID_WPS_HTTP_REQ Peer Buffer wps_http_req_var_struct,
42、2018/4/12,Copyright MediaTek Inc. All rights reserved.,45,MSG_ID_WPS_READ_CONTENT_REQ Local Parameter wps_read_content_req_struct,2018/4/12,Copyright MediaTek Inc. All rights reserved.,46,MSG_ID_WPS_POST_CONTENT_RESLocal Parameter wps_post_content_res_struct,2018/4/12,Copyright MediaTek Inc. All rig
43、hts reserved.,47,MSG_ID_WPS_POST_CONTENT_RESPeer Buffer wps_post_content_res_var_struct,2018/4/12,Copyright MediaTek Inc. All rights reserved.,48,Message Flow: GET,Scenario: application wants to get a file using WPS serviceStep 1: setup a channelStep 2: send the HTTP requestStep 3: receive the replyStep 4: Remove the channelInvolved ILM :MSG_ID_WPS_SET_CHANNEL_REQMSG_ID_WPS_SET_CHANNEL_RSPMSG_ID_WPS_HTTP_REQMSG_ID_WPS_HTTP_RSPMSG_ID_WPS_READ_CONTENT_REQMSG_ID_WPS_READ_CONTENT_RSPMSG_ID_WPS_UNSET_CHANNEL_REQMSG_ID_WPS_UNSET_CHANNEL_RSP,