void SEC_InitializeNetworking(uint_32 pcbs, uint_32 msgs, uint_32 sockets, boolean dhcp) { int_32 error; IPCFG_IP_ADDRESS_DATA ip_data; _enet_address enet_address; /* runtime RTCS configuration */ _RTCSPCB_init = pcbs; _RTCS_msgpool_init = msgs; _RTCS_socket_part_init = sockets; error = RTCS_create(); if (error == RTCS_OK) { ip_data.ip = ENET_IPADDR; ip_data.mask = ENET_IPMASK; ip_data.gateway = ENET_IPGATEWAY; ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, enet_address); ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_address); ipcfg_add_dns_ip(BSP_DEFAULT_ENET_DEVICE,LWDNS_server_ipaddr); // check link status printf("\nWaiting for ethernet cable plug in ... "); while(!ipcfg_get_link_active(BSP_DEFAULT_ENET_DEVICE)) {}; printf("Cable connected\n"); /* If DHCP Enabled, get IP address from DHCP server */ if (dhcp) { printf("\nDHCP bind ... "); error = ipcfg_bind_dhcp_wait(BSP_DEFAULT_ENET_DEVICE, 1, &ip_data); if (error != IPCFG_ERROR_OK) { printf("Error %08x!\n", error); } else { printf("Successful!\n"); } } else { /* Else bind with static IP */ printf ("\nStatic IP bind ... "); error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data); if (error != IPCFG_ERROR_OK) { printf("Error %08x!\n",error); } else { printf("Successful!\n"); } } if (error == IPCFG_ERROR_OK) { ipcfg_get_ip(BSP_DEFAULT_ENET_DEVICE, &ip_data); printf("\nIP Address : %d.%d.%d.%d\n",IPBYTES(ip_data.ip)); printf("\nSubnet Address : %d.%d.%d.%d\n",IPBYTES(ip_data.mask)); printf("\nGateway Address : %d.%d.%d.%d\n",IPBYTES(ip_data.gateway)); printf("\nDNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE,0))); } } else { printf("\nRTCS_Create failed !\n"); _task_block(); } }
boolean init_network() { IPCFG_IP_ADDRESS_DATA ip_data; _enet_address enet_address; uint_32 error; _RTCS_socket_part_init = RTCS_NUMBER_OF_SOCKETS; _RTCS_socket_part_grow = 2; _RTCS_socket_part_max = 20; error = RTCS_create(); if (error != RTCS_OK) { printf("init_network: RTCS failed to initialize, error = %X", error); _task_block(); } _IP_forward = TRUE; ip_data.ip = ENET_IPADDR; ip_data.mask = ENET_IPMASK; // we dont need the gateway adress for the netgear-switch //ip_data.gateway = ENET_GATEWAY; // calculate the mac-adress ENET_get_mac_address (ENET_DEVICE, ENET_IPADDR, enet_address); // initialize the device error = ipcfg_init_device (ENET_DEVICE, enet_address); if (error != RTCS_OK) { printf("\nFailed to initialize ethernet device, error = %X", error); _task_block(); } #if RTCSCFG_ENABLE_LWDNS LWDNS_server_ipaddr = ENET_GATEWAY; ipcfg_add_dns_ip(ENET_DEVICE,LWDNS_server_ipaddr); #endif /* RTCSCFG_ENABLE_LWDNS */ printf("Setting static IP address ... "); error = ipcfg_bind_staticip (ENET_DEVICE, &ip_data); if (error != IPCFG_ERROR_OK) { printf("\nRTCS failed to bind interface with IPv4, error = %X", error); _task_block(); } else printf("OK\n"); ipcfg_get_ip(ENET_DEVICE, &ip_data); printf("IP Address : %d.%d.%d.%d\n",IPBYTES(ip_data.ip)); printf("Subnet Address : %d.%d.%d.%d\n",IPBYTES(ip_data.mask)); printf("Gateway Address : %d.%d.%d.%d\n",IPBYTES(ip_data.gateway)); printf("DNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(ENET_DEVICE,0))); printf("MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n", enet_address[0], enet_address[1], enet_address[2], enet_address[3], enet_address[4], enet_address[5]); return TRUE; }
static int32_t Shell_ipconfig_status (uint32_t enet_device) { IPCFG_STATE state; bool link; bool task; _enet_address mac = {0}; IPCFG_IP_ADDRESS_DATA ip_data ; #if RTCSCFG_IPCFG_ENABLE_DNS _ip_address dns; #endif #if RTCSCFG_IPCFG_ENABLE_BOOT _ip_address tftp_serveraddress; unsigned char *tftp_servername; unsigned char *boot_filename; #endif #if RTCSCFG_ENABLE_IP6 uint32_t result; uint32_t n; IPCFG6_GET_ADDR_DATA data; char prn_addr6[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; #endif state = ipcfg_get_state (enet_device); if (state != IPCFG_STATE_INIT) { link = ipcfg_get_link_active (enet_device); task = ipcfg_task_status (); ipcfg_get_mac (enet_device, mac); ipcfg_get_ip (enet_device, &ip_data); #if RTCSCFG_IPCFG_ENABLE_DNS dns = ipcfg_get_dns_ip (enet_device, 0); #endif #if RTCSCFG_IPCFG_ENABLE_BOOT tftp_serveraddress = ipcfg_get_tftp_serveraddress (enet_device); tftp_servername = ipcfg_get_tftp_servername (enet_device); boot_filename = ipcfg_get_boot_filename (enet_device); #endif printf ("Eth# : %d\n", enet_device); printf ("Link : %s\n", link ? "on" : "off"); printf ("MAC : %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); #if RTCSCFG_ENABLE_IP4 printf ("IP4 : %d.%d.%d.%d Type: %s\n", IPBYTES(ip_data.ip), ipcfg_get_state_string (state) ); printf ("IP4 Mask : %d.%d.%d.%d\n", IPBYTES(ip_data.mask)); #if RTCSCFG_ENABLE_GATEWAYS printf ("IP4 Gate : %d.%d.%d.%d\n", IPBYTES(ip_data.gateway)); #endif #if RTCSCFG_IPCFG_ENABLE_DNS printf ("IP4 DNS : %d.%d.%d.%d\n", IPBYTES(dns)); #endif #endif /* RTCSCFG_ENABLE_IP4 */ /************************************************/ #if RTCSCFG_ENABLE_IP6 n = 0; while(!ipcfg6_get_addr(enet_device, n, &data)) { if(inet_ntop(AF_INET6,&data.ip_addr, prn_addr6, sizeof(prn_addr6))) { printf("IP6 : %s ",prn_addr6); /* IPv6 address state */ printf("State: "); switch(data.ip_addr_state) { case IP6_ADDR_STATE_PREFERRED: printf("PREFERRED "); break; case IP6_ADDR_STATE_TENTATIVE: printf("TENTATIVE "); break; case IP6_ADDR_STATE_NOT_USED: printf("NOT_USED "); break; default: printf("%d ", data.ip_addr_state); break; } /* IPv6 address type */ printf("Type: "); switch(data.ip_addr_type) { case IP6_ADDR_TYPE_MANUAL: printf("MANUAL\n"); break; case IP6_ADDR_TYPE_AUTOCONFIGURABLE: printf("AUTOCONFIGURABLE\n"); break; default: printf("%d\n", data.ip_addr_type); break; } } else { printf("IP6 : ERROR IP CONVERT\n"); } n++; } /* Print Scope ID.*/ printf("ScopeID : %d\n", ipcfg6_get_scope_id(enet_device)); #endif /* RTCSCFG_ENABLE_IP6 */ #if RTCSCFG_ENABLE_IP4 #if RTCSCFG_IPCFG_ENABLE_BOOT printf ("TFTP: %d.%d.%d.%d '%s'\n", IPBYTES(tftp_serveraddress), tftp_servername); printf ("BOOT: '%s'\n", boot_filename); #endif #endif /* RTCSCFG_ENABLE_IP4 */ printf ("Link status task %s\n", task ? "running" : "stopped"); } else { printf ("Ethernet device %d not yet initialized.\n", enet_device); } return SHELL_EXIT_SUCCESS; }
void SEC_InitializeNetworking(uint_32 pcbs, uint_32 msgs, uint_32 sockets, boolean dhcp) { int_32 error; IPCFG_IP_ADDRESS_DATA ip_data; _enet_address enet_address; #if PSP_MQX_CPU_IS_MCF51CN _enet_handle ehandle; _rtcs_if_handle ihandle; #endif /* runtime RTCS configuration */ _RTCSPCB_init = pcbs; _RTCS_msgpool_init = msgs; _RTCS_socket_part_init = sockets; error = RTCS_create(); if (error == RTCS_OK) { ip_data.ip = ENET_IPADDR; ip_data.mask = ENET_IPMASK; ip_data.gateway = ENET_IPGATEWAY; #if RTCSCFG_ENABLE_LWDNS LWDNS_server_ipaddr = ENET_IPDNS; #endif ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, enet_address); #if PSP_MQX_CPU_IS_MCF51CN ENET_initialize_ex(&ENET_param, BSP_DEFAULT_ENET_DEVICE, &ehandle); if (RTCS_if_add(ehandle, RTCS_IF_ENET, &ihandle) == 0) { if (ipcfg_init_interface(-1, ihandle) != 0) { RTCS_if_remove(ihandle); ENET_shutdown(ehandle); _task_block(); } } else { ENET_shutdown(ehandle); } #else ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_address); #endif ipcfg_add_dns_ip(BSP_DEFAULT_ENET_DEVICE,LWDNS_server_ipaddr); // check link status printf("\nWaiting for ethernet cable plug in ... "); while(!ipcfg_get_link_active(BSP_DEFAULT_ENET_DEVICE)) {}; printf("Cable connected\n"); /* If DHCP Enabled, get IP address from DHCP server */ if (dhcp) { printf("\nDHCP bind ... "); error = ipcfg_bind_dhcp_wait(BSP_DEFAULT_ENET_DEVICE, 1, &ip_data); if (error != IPCFG_ERROR_OK) { printf("Error %08x!\n", error); } else { printf("Successful!\n"); } } else { /* Else bind with static IP */ printf ("\nStatic IP bind ... "); error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data); if (error != IPCFG_ERROR_OK) { printf("Error %08x!\n",error); } else { printf("Successful!\n"); } } if (error == IPCFG_ERROR_OK) { ipcfg_get_ip(BSP_DEFAULT_ENET_DEVICE, &ip_data); printf("\nIP Address : %d.%d.%d.%d\n",IPBYTES(ip_data.ip)); printf("\nSubnet Address : %d.%d.%d.%d\n",IPBYTES(ip_data.mask)); printf("\nGateway Address : %d.%d.%d.%d\n",IPBYTES(ip_data.gateway)); printf("\nDNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE,0))); } } else { printf("\nRTCS_Create failed !\n"); _task_block(); } SEC_GetTime(); }
void SEC_EmailAlert() { MQX_TICK_STRUCT ticks; MQX_XDATE_STRUCT xdate; sockaddr addr; _ip_address ipaddr; uint_32 sock; uint_32 error; uint_32 option; char_ptr event_ptr; char temp[150]; char domain[30]; char user[30]; #if DEMOCFG_AUTH_REQUIRED char temp_encoded[40]; #endif if (!RTCS_resolve_ip_address(EMAIL_SERVER,&ipaddr,NULL,0)) { printf("Error resolving IP address for %s using DNS Server at %d.%d.%d.%d\n", EMAIL_SERVER, IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE, 0))); return; } /* Create socket */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == RTCS_SOCKET_ERROR) { printf("Error creating socket\n"); return; } /* Reduce buffer size of socket to save memory */ option = SMTP_BUFFER_SIZE; error = setsockopt(sock, SOL_TCP, OPT_TBSIZE, &option, sizeof(option)); option = SMTP_BUFFER_SIZE; error = setsockopt(sock, SOL_TCP, OPT_RBSIZE, &option, sizeof(option)); option = 1000; error = setsockopt(sock, SOL_TCP, OPT_TIMEWAIT_TIMEOUT, &option, sizeof(option)); /* Allow binding to any address */ #if RTCSCFG_ENABLE_IP4 ((sockaddr_in*)(&addr))->sin_family = AF_INET; ((sockaddr_in*)(&addr))->sin_port = 0; ((sockaddr_in*)(&addr))->sin_addr.s_addr = INADDR_ANY; #elif RTCSCFG_ENABLE_IP6 printf("\nThis application don't support IPv6 only.\n"); _task_block(); #endif error = bind(sock, &addr, sizeof(addr)); if (error != RTCS_OK) { printf("Error in binding socket %08x\n",error); shutdown(sock, FLAG_ABORT_CONNECTION); return; } /* Connect to SMTP server */ #if RTCSCFG_ENABLE_IP4 ((sockaddr_in*)(&addr))->sin_port = IPPORT_SMTP; ((sockaddr_in*)(&addr))->sin_addr.s_addr = ipaddr; #elif RTCSCFG_ENABLE_IP6 printf("\nThis application does not support IPv6 only.\n"); _task_block(); #endif error = connect(sock, &addr, sizeof(addr)); if (error != RTCS_OK) { printf("Error in connection %08x\n",error); shutdown(sock, FLAG_ABORT_CONNECTION); return; } printf("\nConnecting to SMTP server %s...\n",EMAIL_SERVER); printf("SMTP Server Response:\n"); SMTP_Receive_string(sock, temp, sizeof(temp)); /* Follow SMTP protocol to send email */ /* Parse domain name from the FROM address, for use with the EHLO or HELO commands */ sscanf(EMAIL_FROM,"%30s@%30s",user,domain); #if DEMOCFG_AUTH_REQUIRED /* If Authentification is required, use EHLO command */ SMTP_Send_command(sock, "EHLO", domain, temp, sizeof(temp)); SMTP_Send_command(sock,"AUTH LOGIN",NULL,temp,sizeof(temp)); /* Find base64 econding of login name and send to SMTP server */ sprintf(temp,"%s\r\n",base64_encode(AUTH_USERNAME,temp_encoded)); SMTP_Send_string(sock,temp); SMTP_Receive_string(sock,temp,sizeof(temp)-1); /* Find base64 econding of password and send to SMTP server */ sprintf(temp,"%s\r\n",base64_encode(AUTH_PASSWORD,temp_encoded)); SMTP_Send_string(sock,temp); SMTP_Receive_string(sock,temp, sizeof(temp)-1); #else /* If Authentification is not required, use HELO command as not all servers recognize EHLO */ SMTP_Send_command(sock, "HELO", domain, temp, sizeof(temp)); #endif /* Transfer sender email address to server */ SMTP_Send_command(sock,"MAIL FROM:","<" EMAIL_FROM ">" ,temp,sizeof(temp)); /* Transfer recipient email address to server */ SMTP_Send_command(sock,"RCPT TO:","<" EMAIL_TO ">" ,temp,sizeof(temp)); /* Start DATA section */ SMTP_Send_command(sock,"DATA",NULL,temp,sizeof(temp)); printf("Sent:\n"); /* Send Subject, To, and From fields */ SMTP_Send_string(sock,(pointer)Email_header); /* Determine what to send as body of message */ _time_get_ticks(&ticks); _time_ticks_to_xdate(&ticks,&xdate); sprintf(temp, "Date: %s, %d %s %d %02d:%02d:%02d -0000\r\n\r\n", wday[xdate.WDAY],xdate.MDAY, months[xdate.MONTH-1],xdate.YEAR, xdate.HOUR, xdate.MIN, xdate.SEC ); SMTP_Send_string(sock,temp); /* If button pushed was the "Door"... */ if(SEC_Params.Status==SEC_DOOR_OPEN_STATUS) { event_ptr = "Door was opened "; } else /* If button pushed was the "Window"... */ if(SEC_Params.Status==SEC_WINDOW_OPEN_STATUS) { event_ptr = "Window was opened "; } /* If not sure what caused stop mode to exit... */ else { event_ptr = "Unknown event occurred "; } SMTP_Send_string(sock,event_ptr); /* If SNTP enabled, then send exact date and time button press happened */ #if DEMOCFG_ENABLE_SNTP sprintf(temp,"on %d/%d/%d at %02d:%02d:%02d GMT. \r\n",xdate.MONTH,xdate.MDAY,xdate.YEAR,xdate.HOUR,xdate.MIN,xdate.SEC); /* If SNTP not enabled, then give elapsed time instead */ #else sprintf(temp,"%02d:%02d:%02d after system was started. \r\n",xdate.HOUR,xdate.MIN,xdate.SEC); #endif SMTP_Send_string(sock,temp); if (last[0]) { /* Send out how long ago the last event was */ SMTP_Send_string(sock,"Previous event was "); SMTP_Send_string(sock,last); } strcpy(last,temp); /* Send Freescale signature and disconnect from server with QUIT command */ SMTP_Send_string(sock,"\r\n-Freescale Semiconductor\r\n.\r\n"); SMTP_Send_command(sock,"QUIT",NULL,temp,sizeof(temp)); /* Close socket */ shutdown(sock, FLAG_CLOSE_TX); }
void rtcs_init() { IPCFG_IP_ADDRESS_DATA ip_data; _enet_address enet_address; uint_32 error = RTCS_create(); if (error != RTCS_OK) { printf("\nRTCS failed to initialize, error = %X", error); _task_block(); } _IP_forward = TRUE; #ifdef BSP_ENET_DEVICE_COUNT #if (BSP_ENET_DEVICE_COUNT > 0) /*Following section is relevant only in case there as some enet driver available in BSP (FEC, MACNET, WIFI or other enet driver)*/ ip_data.ip = ENET_IPADDR; ip_data.mask = ENET_IPMASK; ip_data.gateway = ENET_GATEWAY; /* calcualte unique mac address from IP ADDRES */ ENET_get_mac_address (ENET_DEVICE, ENET_IPADDR, enet_address); error = ipcfg_init_device (ENET_DEVICE, enet_address); if (error != RTCS_OK) { printf("\nFailed to initialize ethernet device, error = %X", error); _task_block(); } #if RTCSCFG_ENABLE_LWDNS LWDNS_server_ipaddr = ENET_GATEWAY; ipcfg_add_dns_ip(ENET_DEVICE,LWDNS_server_ipaddr); #endif /* RTCSCFG_ENABLE_LWDNS */ printf("\nWaiting for ethernet cable plug in ... "); while(!ipcfg_get_link_active(ENET_DEVICE)) {}; printf("Cable connected\n"); #if RTCS_DHCP printf("Contacting DHCP server ... "); error = ipcfg_bind_dhcp_wait(ENET_DEVICE, FALSE, &ip_data); #else printf("Setting static IP address ... "); error = ipcfg_bind_staticip (ENET_DEVICE, &ip_data); #endif /* RTCS_DHCP */ if (error != IPCFG_ERROR_OK) { printf("\nRTCS failed to bind interface with IPv4, error = %X", error); _task_block(); } else { printf("OK\n"); } ipcfg_get_ip(ENET_DEVICE, &ip_data); printf("\nIP Address : %d.%d.%d.%d\n",IPBYTES(ip_data.ip)); printf("\nSubnet Address : %d.%d.%d.%d\n",IPBYTES(ip_data.mask)); printf("\nGateway Address : %d.%d.%d.%d\n",IPBYTES(ip_data.gateway)); printf("\nDNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(ENET_DEVICE,0))); #endif /* BSP_ENET_DEVICE_COUNT > 0 */ #endif /* BSP_ENET_DEVICE_COUNT */ #if RTCS_PPP PPP_start(); #endif /* RTCS_PPP */ /* TODO: start server(s) in separate tasks */ }