C语言检查字符串是否为合法的IPv4地址, 并解析该地址函数

为了实现这个功能, 今天写了一个, 在这里记录下来.

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;

static uint8_t is_ip_address(uint8_t* host, uint8_t ipBuf[4]){
    uint8_t* p=host;
    uint8_t fieldIndex=0;
    uint8_t numberNum=0;
    uint16_t fieldValue=0;
    while(*p){
        if(*p>='0' && *p<='9'){
            numberNum++;
            fieldValue*=10;
            fieldValue+=(*p-'0');
            if(fieldValue>255) return 0;
        }
        else if(*p=='.'){
            if(numberNum==0) return 0;
            ipBuf[fieldIndex]=fieldValue;
            fieldIndex++;
            if(fieldIndex>=4) return 0;
            numberNum=fieldValue=0;
        }
        else return 0;
        p++;
    }
    if(fieldIndex!=3 || numberNum==0) return 0;
    ipBuf[3]=fieldValue;
    return 1;
}

wordpress 快速搭建企业网站

1. 后台内容要能添加, 通过add_meta_box实现
在functions.php中添加如下代码

function post_meta_boxes_setup() {
	/* Add meta boxes on the 'add_meta_boxes' hook. */
	add_action( 'add_meta_boxes', 'add_post_meta_boxes' );
	/* Save post meta on the 'save_post' hook. */
	add_action( 'save_post', 'save_post_source_meta', 10, 2 );

}
function add_post_meta_boxes() {
	add_meta_box(
		'post-source', // Unique ID
		esc_html__( '文章来源/作者', 'low-text' ), // Title
		'post_source_meta_box', // Callback function
		'post', // Admin page (or post type)
		'normal', // Context
		'high' // Priority
	);
}
function post_source_meta_box( $object, $box ) { ?>

	<?php wp_nonce_field( basename( __FILE__ ), 'post_source_nonce' ); ?>

	<p>
		<label>文章来源</label><br/>
		<input class="widefat" type="text" name="post-source" id="post-source" value="<?php echo esc_html__( get_post_meta( $object->ID, 'post_source', true ) ); ?>" size="30" />
		<label>文章作者</label><br/>
		<input class="widefat" type="text" name="post-author" id="post-autohr" value="<?php echo esc_html__( get_post_meta( $object->ID, 'post_author', true ) ); ?>" size="30" />
	</p>

<?php }

function save_post_source_meta( $post_id, $post ) {

	/* Verify the nonce before proceeding. */
	if ( !isset( $_POST['post_source_nonce'] ) || !wp_verify_nonce( $_POST['post_source_nonce'], basename( __FILE__ ) ) )
		return $post_id;

	/* Get the post type object. */
	$post_type = get_post_type_object( $post->post_type );

	/* Check if the current user has permission to edit the post. */
	if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
		return $post_id;

	/* Get the posted data and sanitize it for use as an HTML class. */
	$new_meta_value = ( isset( $_POST['post-source'] ) ? balanceTags( $_POST['post-source'] ) : '' );

	/* Get the meta key. */
	$meta_key = 'post_source';

	/* Get the meta value of the custom field key. */
	$meta_value = get_post_meta( $post_id, $meta_key, true );

	/* If a new meta value was added and there was no previous value, add it. */
	if ( $new_meta_value && '' == $meta_value )
		add_post_meta( $post_id, $meta_key, $new_meta_value, true );

	/* If the new meta value does not match the old value, update it. */
	elseif ( $new_meta_value && $new_meta_value != $meta_value )
		update_post_meta( $post_id, $meta_key, $new_meta_value );

	/* If there is no new meta value but an old value exists, delete it. */
	elseif ( '' == $new_meta_value && $meta_value )
		delete_post_meta( $post_id, $meta_key, $meta_value );




	/* Get the posted data and sanitize it for use as an HTML class. */
	$new_meta_value = ( isset( $_POST['post-author'] ) ? balanceTags( $_POST['post-author'] ) : '' );

	/* Get the meta key. */
	$meta_key = 'post_author';

	/* Get the meta value of the custom field key. */
	$meta_value = get_post_meta( $post_id, $meta_key, true );

	/* If a new meta value was added and there was no previous value, add it. */
	if ( $new_meta_value && '' == $meta_value )
		add_post_meta( $post_id, $meta_key, $new_meta_value, true );

	/* If the new meta value does not match the old value, update it. */
	elseif ( $new_meta_value && $new_meta_value != $meta_value )
		update_post_meta( $post_id, $meta_key, $new_meta_value );

	/* If there is no new meta value but an old value exists, delete it. */
	elseif ( '' == $new_meta_value && $meta_value )
		delete_post_meta( $post_id, $meta_key, $meta_value );
}

在wp-admin/post.php, wp-admin/post-new.php中靠前的地方, 调用勾子函数

post_meta_boxes_setup();

Continue reading “wordpress 快速搭建企业网站”

网络继电器

这是一款四路的网络继电器, 本来之前设计好的那款, 没有加锁存, 这一款是前几天拿去重新生产的带锁存的版本,通过网络控制四路继电器的开和关。
最近电路板做的频率有点高,这款出来后会暂时降低电路板的出品频率,因为软件的框架还需要继续完善。

wifi版的红外学习控制器

之前的网络红外学习控制器, 为我自己实现了卧室空调和风扇的自动控制(我还特地买了红外控制的风扇), 再也不用一大清早被空调冷醒; 或者定时关空调又会热醒的问题。

然后我又做了wifi版的, 通过wifi接入互联网, 省掉很多主板空间, 整个wifi功能所占的空间跟一个RJ45占的空间差不多.

焊接的时候还遇到问题, 第一块板子不知道哪里没焊好, 主控芯片总是不能正常运行, 还好, 第二块成功了, 主控和wifi都没问题

RFID卡存取控制, 笔记

RFID卡每个区分4个块, 前3个块为数据块, 第4个块为控制块, 控制块的结构如下:
密码A(6字节)|存取控制码(4字节)|密码B(6字节)

 

以下转自: http://blog.sina.com.cn/s/blog_9ed067ad01010i4v.html

S50和S70的块分为数据块和控制块,对数据块的操作有“读”、“写”、“加值”、“减值(含传输和存储)”四种,对控制块的操作只有“读”和“写”两种。

S50和S70的每个扇区有两组密码KeyA和KeyB,所谓的“条件”就是针对这两组密码而言,包括“验证密码A可以操作(KeyA)”、“验证密码B可以操作(KeyB)”、“验证密码A或密码B都可以操作(KeyA|B)”、“验证哪个密码都不可以操作(Never)”四种条件。

这些“条件”和“操作”的组合被分成8种情况,正好可以用3位二进制数(C1、C2、C3)来表示。

数据块(每个扇区除区尾块之外的块)的存取控制如下表所示:

控制块(每个扇区的区尾块)的存取控制如下表所示:

S50的每个扇区有4个块,这四个块的存取控制是相互独立的,每个块需要3个bit,四个块共使用12个bit。在保存的时候,为了防止控制位出错,同时保存了这12个bit的反码,这样一个区的存储控制位在保存时共占用24bit的空间,正好是3个字节。我们前面说存取控制字有四个字节(区尾块的Byte6~Byte9),实际上只使用的Byte6、Byte7和Byte8,Byte9没有用,用户可以把Byte9作为普通存储空间使用。各块控制位存储格式如下:

由于出厂时数据块控制位的默认值是C1C2C3=000,控制块的默认值是C1C2C3=001,而Byte9一般是69H,所以出厂白卡的控制字通常是FF078069H.

上周做的小东西

上周五连续向工厂发了两块板, 其中一块是wifi板, 另一块是继电器的板子.
今天到货立马把wifi的焊上测试, 一切OK, wifi芯片使用的是esp8266, 是我目前焊过最小的片子, 还不错,感觉比stm32的片子还要好焊一点.

继电器的板子因为提交工厂过急, 忘了加上锁存芯片(明明就在待办事项里写好要加的,结果忘了), 也不想去焊接它了, 下次重新改版后再焊吧.

成功改掉一个硬件BUG

昨天晚上搞了很久, STM32的独立看门狗就是无法让系统复位, 于是在网上找到了答案, 网上说复位电路使用10K电阻和104电容就可以, 我用的是100K电阻加104电容.
尝试换上10K电阻, 配合104电容真的可以完美实现看门狗复位。

再来思考原因, 因为我使用的是100K上拉电阻和104的电容, 导致RC电路的充电放电时间长, 长于看门狗的下拉时间, 从而看门狗的下拉动作被电容稳压了。