⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webport_8c-source.html

📁 针对AVR单片机开发的嵌入式操作系统
💻 HTML
📖 第 1 页 / 共 3 页
字号:
00145 <span class="preprocessor"></span><span class="comment"></span>00146 <span class="comment">/*!</span>00147 <span class="comment"> * \addtogroup xgWebPort</span>00148 <span class="comment"> */</span><span class="comment"></span>00149 <span class="comment">/*@{*/</span>00150 <span class="comment"></span>00151 <span class="comment">/*!</span>00152 <span class="comment"> * \brief CPU reset macro.</span>00153 <span class="comment"> *</span>00154 <span class="comment"> * Start all over on fatal initialization errors.</span>00155 <span class="comment"> *</span>00156 <span class="comment"> * \note This function currently works on AVR systems only. On other </span>00157 <span class="comment"> *       architectures an endless loop will be entered.</span>00158 <span class="comment"> */</span>00159 <span class="preprocessor">#if defined(__AVR_ATmega103__) || defined(__AVR_ATmega128__)</span><a name="l00160"></a><a class="code" href="group__xg_web_port.html#ga3">00160</a> <span class="preprocessor"></span><span class="preprocessor">#define JUMP_RESET { asm("cli"); asm("call 0"); }</span>00161 <span class="preprocessor"></span><span class="preprocessor">#else</span>00162 <span class="preprocessor"></span><span class="preprocessor">#define JUMP_RESET for(;;)</span>00163 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00164 <span class="preprocessor"></span><span class="comment"></span>00165 <span class="comment">/*!</span>00166 <span class="comment"> * \brief Process a single HTTP request.</span>00167 <span class="comment"> *</span>00168 <span class="comment"> * This routine performs the whole cycle of a HTTP request.</span>00169 <span class="comment"> *</span>00170 <span class="comment"> * - Creating a socket.</span>00171 <span class="comment"> * - Listening on the defined port.</span>00172 <span class="comment"> * - Processing the request.</span>00173 <span class="comment"> * - Closing the socket.</span>00174 <span class="comment"> */</span><a name="l00175"></a><a class="code" href="group__xg_web_port.html#ga0">00175</a> <span class="keywordtype">void</span> <a class="code" href="group__xg_web_port.html#ga0">Service</a>(<span class="keywordtype">void</span>)00176 {00177     TCPSOCKET *sock;            <span class="comment">/* TCP socket pointer. */</span>00178     FILE *stream;               <span class="comment">/* TCP stream. */</span>00179     u_long to = 5000;00180 00181     <span class="comment">/*</span>00182 <span class="comment">     * Let the Nut/OS library create a TCP socket for us.</span>00183 <span class="comment">     */</span>00184     <span class="keywordflow">if</span> ((sock = NutTcpCreateSocket()) != 0) {00185 00186         NutTcpSetSockOpt(sock, SO_RCVTIMEO, &amp;to, <span class="keyword">sizeof</span>(to));00187 00188         <span class="comment">/*</span>00189 <span class="comment">         * Listen on the port defined in webport.h. Typically this is</span>00190 <span class="comment">         * port 80.</span>00191 <span class="comment">         */</span>00192         <span class="keywordflow">if</span> (NutTcpAccept(sock, <a class="code" href="group__xg_w_p_defs.html#ga4">HTTP_PORT</a>) == 0) {00193             <span class="comment">/*</span>00194 <span class="comment">             * The Nut/OS memory manager dynamically allocates and </span>00195 <span class="comment">             * releases SRAM space. In this application most memory is </span>00196 <span class="comment">             * used by incoming telegrams and the socket interface. If </span>00197 <span class="comment">             * the available space becomes low, we sleep for 200 </span>00198 <span class="comment">             * milliseconds and try again. This way we will always leave </span>00199 <span class="comment">             * some RAM for more important parts of the system.</span>00200 <span class="comment">             *</span>00201 <span class="comment">             * The watermark is defined in webport.h.</span>00202 <span class="comment">             */</span>00203             <span class="keywordflow">while</span> (NutHeapAvailable() &lt; <a class="code" href="group__xg_w_p_defs.html#ga5">LOW_MEM_MARK</a>) {00204                 NutSleep(200);00205             }00206 00207             <span class="comment">/*</span>00208 <span class="comment">             * Create a stream from the socket, so we can use stdio.</span>00209 <span class="comment">             */</span>00210             <span class="keywordflow">if</span> ((stream = _fdopen((<span class="keywordtype">int</span>) sock, <span class="stringliteral">"r+b"</span>)) != 0) {00211                 <span class="comment">/*</span>00212 <span class="comment">                 * Process HTTP request. This routine is part of the</span>00213 <span class="comment">                 * Nut/OS libraries and greatly simplifies our application</span>00214 <span class="comment">                 * code. It will automatically read requested files from</span>00215 <span class="comment">                 * the file system and send them to the browser or</span>00216 <span class="comment">                 * call the proper CGI routines.</span>00217 <span class="comment">                 */</span>00218                 NutHttpProcessRequest(stream);00219 00220                 <span class="comment">/*</span>00221 <span class="comment">                 * Close the stream to release its resources.</span>00222 <span class="comment">                 */</span>00223                 fclose(stream);00224             }00225         }00226 00227         <span class="comment">/*</span>00228 <span class="comment">         * Close the socket. If the client didn't close the connection yet,</span>00229 <span class="comment">         * it will now be forced to do so.</span>00230 <span class="comment">         */</span>00231         NutTcpCloseSocket(sock);00232     }00233 }00234 <span class="comment"></span>00235 <span class="comment">/*! \fn ServiceThread(void *arg)</span>00236 <span class="comment"> * \brief Background thread to process HTTP requests.</span>00237 <span class="comment"> *</span>00238 <span class="comment"> * This thread calls Service() in an endless loop. It can be started more</span>00239 <span class="comment"> * than once to support parallel connections.</span>00240 <span class="comment"> */</span><a name="l00241"></a><a class="code" href="group__xg_web_port.html#ga1">00241</a> THREAD(ServiceThread, arg)00242 {00243     <span class="comment">/*</span>00244 <span class="comment">     * Loop endless for connections.</span>00245 <span class="comment">     */</span>00246     <span class="keywordflow">for</span> (;;) {00247         <a class="code" href="group__xg_web_port.html#ga0">Service</a>();00248     }00249 }00250 <span class="comment"></span>00251 <span class="comment">/*!</span>00252 <span class="comment"> * \brief Main entry of our application.</span>00253 <span class="comment"> *</span>00254 <span class="comment"> * Nut/OS automatically calls this entry after initialization.</span>00255 <span class="comment"> * </span>00256 <span class="comment"> * This routine will do all required initialization, start some</span>00257 <span class="comment"> * background threads and then process incoming HTTP requests together </span>00258 <span class="comment"> * with the concurrently running background threads.</span>00259 <span class="comment"> */</span><a name="l00260"></a><a class="code" href="group__xg_web_port.html#ga2">00260</a> <span class="keywordtype">int</span> <a class="code" href="group__xg_web_port.html#ga2">main</a>(<span class="keywordtype">void</span>)00261 {00262     u_char i;00263 00264     <span class="comment">/*</span>00265 <span class="comment">     * We may optionally use the serial port for debugging output.</span>00266 <span class="comment">     */</span>00267 <span class="preprocessor">#if defined(WP_STATUSOUT) || defined(NUTDEBUG)</span>00268 <span class="preprocessor"></span>    u_long baud = 115200;00269 00270     <span class="comment">/*</span>00271 <span class="comment">     * All Nut/OS devices must be registered. This serves two purposes.</span>00272 <span class="comment">     * First, it will create a reference to the device driver code, so</span>00273 <span class="comment">     * this is linked to the application code. Second, it will initialze</span>00274 <span class="comment">     * the hardware.</span>00275 <span class="comment">     *</span>00276 <span class="comment">     * Nut/OS offers several device drivers for RS232 communication. The </span>00277 <span class="comment">     * device devDebug0 is prefered for debug output, because it is not</span>00278 <span class="comment">     * only simple, small and fast, but can also used to create output</span>00279 <span class="comment">     * from within interrupt routines.</span>00280 <span class="comment">     */</span>00281     NutRegisterDevice(&amp;devDebug0, 0, 0);00282 00283     <span class="comment">/*</span>00284 <span class="comment">     * Now, after the device is registered, it is known to the system</span>00285 <span class="comment">     * by its name. We can use a standard C library routine to open it</span>00286 <span class="comment">     * as our standard output.</span>00287 <span class="comment">     */</span>00288     freopen(<a class="code" href="group__xg_w_p_defs.html#ga3">WP_STATUSOUT</a>, <span class="stringliteral">"w"</span>, stdout);00289 00290     <span class="comment">/*</span>00291 <span class="comment">     * Nut/OS uses _ioctl() to set device specific parameters, like</span>00292 <span class="comment">     * the baudrate for serial communication.</span>00293 <span class="comment">     */</span>00294     _ioctl(_fileno(stdout), UART_SETSPEED, &amp;baud);00295 00296     <span class="comment">/*</span>00297 <span class="comment">     * Now we can use the devDebug0 device for standard output. Let's</span>00298 <span class="comment">     * print a banner including the Nut/OS version we are running.</span>00299 <span class="comment">     */</span>00300     printf(<span class="stringliteral">"\n\nNut/OS %s Webport Daemon\n"</span>, NutVersionString());00301 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -