OSDN Git Service

APIPAの為のAPI追加
[mimic/MiMicSDK.git] / lib / src / net / NyLPC_cNetConfig.c
1 /*********************************************************************************\r
2  * PROJECT: MiMic\r
3  * --------------------------------------------------------------------------------\r
4  *\r
5  * This file is part of MiMic\r
6  * Copyright (C)2011 Ryo Iizuka\r
7  *\r
8  * MiMic is free software: you can redistribute it and/or modify\r
9  * it under the terms of the GNU Lesser General Public License as published\r
10  * by the Free Software Foundation, either version 3 of the License, or\r
11  * (at your option) any later version.\r
12  *\r
13  * This program is distributed in the hope that it will be useful,\r
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16  * GNU General Public License for more details.\r
17  *\r
18  * You should have received a copy of the GNU Lesser General Public License\r
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
20  *\r
21  * For further information please contact.\r
22  *      http://nyatla.jp/\r
23  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
24  *\r
25  *********************************************************************************/\r
26 #include "NyLPC_cNetConfig.h"\r
27 #include "../flash/NyLPC_cMiMicConfiglation.h"\r
28 \r
29 \r
30 static void update(NyLPC_TcNetConfig_t* i_inst,const struct NyLPC_TMimicConfigulation* pdata);\r
31 \r
32 \r
33 void NyLPC_cNetConfig_initialize(NyLPC_TcNetConfig_t* i_inst,NyLPC_TBool i_is_factory_default)\r
34 {\r
35         if(i_is_factory_default)\r
36         {\r
37                 update(i_inst,NyLPC_cMiMicConfiglation_loadFactoryDefault());\r
38         }else{\r
39                 update(i_inst,NyLPC_cMiMicConfiglation_loadFromFlash());\r
40         }\r
41 }\r
42 \r
43 \r
44 /**\r
45  * ユーザコンフィギュレーションを更新する。\r
46  * この関数をコールするときは、割込/FreeRTOSを一時停止すること。\r
47  */\r
48 /*\r
49 NyLPC_TBool NyLPC_cNetConfig_saveToOnchipFlash(NyLPC_TcNetConfig_t* i_inst)\r
50 {\r
51         NyLPC_TUInt8* pdata;\r
52         NyLPC_TcOnchipFlashWriter_t s;\r
53         struct TNetConfigMemMap tmp;\r
54         NyLPC_cOnchipFlashWriter_initialize(&s);\r
55         //書込みデータを作成\r
56         tmp.fast_boot=0xffffffff;\r
57         tmp.version  =i_inst->version;\r
58         tmp.interface_type=NyLPC_cNetConfig_INTERFACE_TYPE_ETHERNET;\r
59         pdata=i_inst->interface_setting.ethernet.eth_mac.addr;\r
60         tmp.mac_00_01_02_03=(((NyLPC_TUInt32)pdata[0])<<24)|(((NyLPC_TUInt32)pdata[1])<<16)|(((NyLPC_TUInt32)pdata[2])<<8)|(((NyLPC_TUInt32)pdata[3])<<0);\r
61         tmp.mac_04_05_xx_xx=(((NyLPC_TUInt32)pdata[4])<<24)|(((NyLPC_TUInt32)pdata[5])<<16);\r
62         tmp.ipv4_addr_net=i_inst->interface_setting.ethernet.ip_addr.v;\r
63         tmp.ipv4_mask_net=i_inst->interface_setting.ethernet.netmask.v;\r
64         tmp.ipv4_drut_net=i_inst->interface_setting.ethernet.dr_addr.v;\r
65 \r
66         //イレース\r
67         if(!NyLPC_cOnchipFlashWriter_elase(&s,MIMIC_CONFIGLATION_FLASH_SECTOR,MIMIC_CONFIGLATION_FLASH_SECTOR)){\r
68                 NyLPC_OnErrorGoto(Error);\r
69         }\r
70         //コンフィギュレーションを書き込む。\r
71         if(!NyLPC_cOnchipFlashWriter_writeSector(&s,MIMIC_CONFIGLATION_FLASH_SECTOR,0x00000000,&tmp,sizeof(struct TNetConfigMemMap))){\r
72                 NyLPC_OnErrorGoto(Error);\r
73         }\r
74         //ユーザコンフィギュレーションをONにする。\r
75         if(setUserConfigulation()){\r
76                 NyLPC_OnErrorGoto(Error);\r
77         }\r
78         NyLPC_cOnchipFlashWriter_finalize(&s);\r
79         return NyLPC_TBool_TRUE;\r
80 Error:\r
81         NyLPC_cOnchipFlashWriter_finalize(&s);\r
82         return NyLPC_TBool_FALSE;\r
83 }\r
84 */\r
85 \r
86 \r
87 /**\r
88  * ETHERNET NETWORKのみなら 1480\r
89  * インターネットを通過するなら 1400程度が妥当。\r
90  */\r
91 //#define ETHERNET_FRAME_LEN 1480\r
92 #define ETHERNET_FRAME_LEN 1400\r
93 static void update(NyLPC_TcNetConfig_t* i_inst,const struct NyLPC_TMimicConfigulation* pdata)\r
94 {\r
95         struct NyLPC_TEthAddr ea;\r
96         struct NyLPC_TIPv4Addr ip,mask,drt;\r
97         //値の読み出し\r
98         ea.addr[0]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>>24)&0xff);\r
99         ea.addr[1]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>>16)&0xff);\r
100         ea.addr[2]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>> 8)&0xff);\r
101         ea.addr[3]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>> 0)&0xff);\r
102         ea.addr[4]=(NyLPC_TUInt8)((pdata->mac_04_05_xx_xx>>24)&0xff);\r
103         ea.addr[5]=(NyLPC_TUInt8)((pdata->mac_04_05_xx_xx>>16)&0xff);\r
104         ip.v=NyLPC_htonl(pdata->ipv4_addr_net);\r
105         mask.v=NyLPC_htonl(pdata->ipv4_mask_net);\r
106         drt.v=NyLPC_htonl(pdata->ipv4_drut_net);\r
107         i_inst->version=1;\r
108         i_inst->interface_type=NyLPC_cNetConfig_INTERFACE_TYPE_ETHERNET;\r
109         NyLPC_cIPv4Config_initialzeForEthernet(&i_inst->interface_setting.ethernet,&ea,ETHERNET_FRAME_LEN);\r
110         NyLPC_cIPv4Config_setDefaultRoute(&i_inst->interface_setting.ethernet,&drt);\r
111         NyLPC_cIPv4Config_setIp(&i_inst->interface_setting.ethernet,&ip,&mask);\r
112         return;\r
113 }\r
114 \r
115 \r
116 \r