2013年5月30日 星期四

ENC28J60+UIP1.0 TCI/IP Stack 接收 UDP 廣播封包

要讓 ENC28J60+UIP1.0 TCI/IP Stack 接收 UDP 廣播封包的方式…

一、UIP 修改 configure 檔 uip-conf.h
    #define UIP_CONF_BROADCAST            1

二、ENC28J60 的部份在設定 FILTER 時要禁用 FILTER。

不接收廣播訊息要設定如下,找到設置 FILTER 的程式碼,mark 掉才能聽廣播…

    //接收過濾器
    //UCEN:單播過濾器使能位
    //當ANDOR = 1 時:
    //1 = 目標地址與本地MAC 地址不匹配的數據包將被丟棄
    //0 = 禁止過濾器
    //當ANDOR = 0 時:
    //1 = 目標地址與本地MAC 地址匹配的數據包會被接受
    //0 = 禁止過濾器

    //CRCEN:後過濾器CRC 校驗使能位
    //1 = 所有CRC 無效的數據包都將被丟棄
    //0 = 不考慮CRC 是否有效
   
    //PMEN:格式匹配過濾器使能位
    //當ANDOR = 1 時:
    //1 = 數據包必須符合格式匹配條件,否則將被丟棄
    //0 = 禁止過濾器
    //當ANDOR = 0 時:
    //1 = 符合格式匹配條件的數據包將被接受
    //0 = 禁止過濾器
    enc28j60Write(ERXFCON, ERXFCON_UCEN|ERXFCON_CRCEN|ERXFCON_PMEN);
    enc28j60Write(EPMM0, 0x3f);
    enc28j60Write(EPMM1, 0x30);
    enc28j60Write(EPMCSL, 0xf9);
    enc28j60Write(EPMCSH, 0xf7);


三、UIP 建立 Connection…
    uip_ipaddr_t ipaddr;  
    struct uip_udp_conn * conn;
   
    uip_ipaddr( ipaddr, 0, 0,0, 0 );
    conn = uip_udp_new( &ipaddr, HTONS( 0 ) );
    if( conn != NULL )
            uip_udp_bind( conn, HTONS( MY_UDP_PORT ) );

2013年5月24日 星期五

socket + pthread 資源佔用情形解決

socket + pthread 資源佔用情形解決

在使用 pthread_create 建立 thread 處理 socket 的時候要注意,縱使 socket close 了,資源還是會被 thread 佔用住,此時要採用分離式 thread,才能在 thread 結束時自動釋放資源,最簡單的方式是建立 thread 後設定為 detach。

    while (1)
    {
        newsockfd = accept(sockfd,
                (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0)
            exit(1);
        pthread_t thread1;
        pthread_create(&thread1, NULL, doSocketThread, &newsockfd );
        pthread_detach(thread1);
    }

2013年5月23日 星期四

Flex Socket Policy

新版的 Flex 支援 socket 通訊,但需要有自己的 Policy Server,好讓 Flex 有連線需求時進行 Policy 認證(透過 port:843)。

一、建立 socketpolicy.pl 檔,內容如下…
############################################################################
#!/usr/bin/perl -wT
#
# Simple Flash Socket Policy Server
# http://www.lightsphere.com/dev/articles/flash_socket_policy.html
#
# Copyright (C) 2008 Jacqueline Kira Hamilton
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .


use Socket;
use IO::Handle;

my $should_be_logging = 1;  # change to 0 to turn off logging.

my $logfile = 'log';

if ($should_be_logging) {
    open(LOG, ">$logfile") or warn "Can't open $logfile: $!\n";
    LOG->autoflush(1);
}

my $port = 843;
my $proto = getprotobyname('tcp');

# start the server:

      &log("Starting server on port $port");
    socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "setsockopt: $!";
      bind(Server,sockaddr_in($port,INADDR_ANY)) or die "bind: $!";
    listen(Server,SOMAXCONN) or die "listen: $!";

    Server->autoflush( 1 );

my $paddr;
&log("Server started. Waiting for connections.");

$/ = "\0";      # reset terminator to null char

# listening loop.

for ( ; $paddr = accept(Client,Server); close Client) {
    Client->autoflush(1);
    my($port,$iaddr) = sockaddr_in($paddr);
    my $ip_address   = inet_ntoa($iaddr);
    my $name         = gethostbyaddr($iaddr,AF_INET) || $ip_address;
    &log( scalar localtime() . ": Connection from $name" );
 
    my $line = ;
    &log("Input: $line");

    if ($line =~ /.*policy\-file.*/i) {
        print Client &xml_policy;
    }
}

sub xml_policy {
    my $str = qq(\0);
    return $str;
}

sub log {
    my($msg) = @_;
    if ($should_be_logging) {
        print LOG $msg,"\n";
    }
}
############################################################################
 
二、在你的 server 上執行…
sudo ./socketpolicy.pl > /dev/null &


2013年5月16日 星期四

Raspberry Pi + Cross Compiler + MySQL

要在 Cross Compiler 上建立 MySQL Connect API 的編譯環境
必須要有 .h include 檔及 mysqlclient lib 檔。

一、先在 Raspberry Pi 下載 MySql connector for C
 wget  http://lgallardo.com/wp-content/uploads/files/programming/mysql-connector-c-6.0.2.tar.gz

二、解壓縮
 tar xvzf mysql-connector-c-6.0.2.tar.gz
 cd mysql-connector-c-6.0.2
 
三、安裝 cmake
 sudo aptitude install cmake
 
四、產出 makefile 檔,其中「install_path」自定義
 cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/install_path 
 
五、make 需要很久的時間
 
六、make install
 
七、確認是否正確安裝
 file /install_path/lib/libmysql.so.16.0.0 
 

八、將 /install_path 底下所有東西 copy 至 cross compiler host 機上。
 

九、範例程式…
    #include <mysql.h>
    MYSQL      *MySQLConRet;
    MYSQL      *MySQLConnection = NULL;
    MySQLConnection = mysql_init( NULL );

    MySQLConRet = mysql_real_connect( MySQLConnection,
                                          hostName,
                                          userId,
                                          password,
                                          db_name,
                                          0,
                                          "/var/run/mysqld/mysqld.sock",
                                          0 );
 
 注意:socket位置預設是 /tmp/mysql.sock,需按你系統所在位置填入正確值否則會出現…
 runtime error:Can't connect to local MySQL server through socket '/tmp/mysql.sock' 
 socket位置可以在 /etc/mysql/my.cnf 或 /etc/mysql/debian.cnf 查詢到。
九、編譯時引入 mysqlclient 函式庫並增加 include 及 lib patch
 gcc client.c -o client  -I/install_path/include -L/install_path/lib -lmysqlclient
 
 ext. links…
http://lgallardo.com/en/2011/07/14/conector-mysql-para-openwrt-mips/
 
=================================================================================
較簡單的方法…
1.安裝開發工具…
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libmysqlclient-dev
 
2.從 pi copy header 檔到開發環境,Eclipse 下增加 include path…
/usr/include/mysql

3.從 pi copy lib 檔到開發環境,Eclipse 下增加 lib path
使用指令查出 lib 位置…
mysql_config --libs
 
查出放在「/usr/lib/arm-linux-gnueabihf」底下,並在 linker command 增加 -lmysqlclient
libmysqlclient.a
libmysqlclient.so 

4.若缺少 libz (compress、uncompress) 亦需一併 copy 過去開發環境,並在 linker command 增加 -lz
libz.a
libz.so




更高效處理 micro second 的方式

更高效處理 micro second 的方式…  以 STM32 為例… __IO unsigned long sys_tick = 0; void SysTick_Handler(void) {     HAL_IncTick();     sys_tick += (SysTi...