<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>engin aydogan &#187; embedded</title>
	<atom:link href="http://engin.bzzzt.biz/tag/embedded/feed/" rel="self" type="application/rss+xml" />
	<link>http://engin.bzzzt.biz</link>
	<description>&#039;s journal</description>
	<lastBuildDate>Thu, 05 Aug 2010 08:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>zero-configuration connectivity on embedded computers</title>
		<link>http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/</link>
		<comments>http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 10:33:39 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[dhcp]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[enda]]></category>
		<category><![CDATA[plc]]></category>
		<category><![CDATA[uip]]></category>

		<guid isPermaLink="false">http://engin.bzzzt.biz/?p=207</guid>
		<description><![CDATA[Our device is networked. It is supposed to: Work when you plug it into any traditional Ethernet network (i.e. with router with embedded DHCP server). It is also supposed to work when you directly connect it to another node (i.e. a computer which you&#8217;re want to configure the device with &#8212; where no DHCP server is available).]]></description>
			<content:encoded><![CDATA[<p>Our device is networked. It is supposed to:</p>
<ul>
<li>Work when you plug it into any traditional Ethernet network (i.e. with router with embedded DHCP server).</li>
<li>It is also supposed to work when you directly connect it to another node (i.e. a computer which you&#8217;re want to configure the device with &#8212; where no DHCP server is available).</li>
</ul>
<p>So our goal is:</p>
<ul>
<li>Zero-configuration connectivity on above two scenarios.</li>
</ul>
<p>Our requirement:</p>
<ul>
<li>Severely scarce resources. Firmware should fit in a 64kb space.</li>
</ul>
<p>Most such networked devices comes with a static IP already set and the node which is supposed to configure them (usually a PC) is supposed to be set to a specific IP address in the same subnet to configure them. This has several drawbacks:</p>
<ul>
<li>Users need to have administration privilleges</li>
<li>Users need to have experience with computers</li>
<li>And this is OS dependent.</li>
</ul>
<p>There are other zero-configuration technology stacks, even though I don&#8217;t have a scientific fact, from their specs I think they&#8217;d take up significant code space.</p>
<p>So here&#8217;s what I&#8217;ve did</p>
<ol>
<li>During device boot I assign it a link-local IP address (i.e. AUTOIP or <a href="http://tools.ietf.org/html/rfc3927">http://tools.ietf.org/html/rfc3927</a>). I did not implement RFC3927 and just generated a fixed IP address according to MAC of the device &#8212; partly because I don&#8217;t have a quality random number generator, as there&#8217;s no clock on most of the devices and reading analog inputs often give same results.</li>
<li>Then I start DHCP negotiations</li>
</ol>
<p>With the scenario the device is able to communicate with another node which also have been assigned to a link-local address without waiting for a DHCP timeout. So when you plug your device directly to your computer you can communicate with the device as soon as your computer also assigned itself a link-local address. This varies from OS to OS but we&#8217;re pleased with the results. Our customers are able to connect to their devices without any configuration.</p>
<p>But there&#8217;s a problem! Even though this configuration worked on our network at the office and many other places where we sold the devices to, it didn&#8217;t work on my home network. It turned out that my linksys router in my home network does not honor DHCP DISCOVER requests if their source IP addresses are not 0.0.0.0</p>
<p>Problem:</p>
<ul>
<li>Some DHCP servers does not honor DHCP DISCOVER requests from 169.254.C.D. They strictly require source IP of the requests to be 0.0.0.0</li>
</ul>
<p>To solve this problem I could either</p>
<ul>
<li>First assign my device IP address 0.0.0.0 and then wait for DHCP timeout, then fallback to AUTO IP. This way DHCP DISCOVER request would work with all routers but other party would have to wait for my device to timeout DHCP and fallback to link-local IP before starting communicating with it &#8212; this is a serious tradeoff.</li>
<li>Or Add UDP IP Spoofing on top of our current implementation hence just make the DHCP DISCOVER source IP 0.0.0.0 and keep the rest the same.</li>
</ul>
<p>Obviously I chose the second approach and patched uIP with the following code:</p>
<pre>$ svn diff
Index: uip/uip.c
===================================================================
--- uip/uip.c   (revision 226)
+++ uip/uip.c   (working copy)
@@ -1181,7 +1181,17 @@
   BUF-&gt;srcport  = uip_udp_conn-&gt;lport;
   BUF-&gt;destport = uip_udp_conn-&gt;rport;

-  uip_ipaddr_copy(BUF-&gt;srcipaddr, uip_hostaddr);
+  extern int g_udp_spoof;
+  extern uip_ipaddr_t g_udp_spoof_ip;
+  if(!g_udp_spoof)
+  {
+    uip_ipaddr_copy(BUF-&gt;srcipaddr, uip_hostaddr);
+  }
+  else
+  {
+       g_udp_spoof = 0;
+       uip_ipaddr_copy(BUF-&gt;srcipaddr, g_udp_spoof_ip);
+  }
   uip_ipaddr_copy(BUF-&gt;destipaddr, uip_udp_conn-&gt;ripaddr);

   uip_appdata = &amp;uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];</pre>
<p>Now our device apparently has best of both worlds.</p>
<ul>
<li>It can communicate with any node with link-local IP address right away at boot time &#8212; without waiting for any sort of (DHCP) timeout.</li>
<li>While link-local communication is working, it tries to acquire an IP address from DHCP server in parallel &#8212; thanks to source IP &#8220;spoofing&#8221;.</li>
</ul>
<p>I&#8217;ll see if further testing will reveal any side effects.</p>
<p>See also:<br />
DHCP RFC: <a href="http://tools.ietf.org/html/rfc2131">http://tools.ietf.org/html/rfc2131</a><br />
Link-local IP Address (AUTO IP): <a href="http://tools.ietf.org/html/rfc3927">http://tools.ietf.org/html/rfc3927</a></p>
]]></content:encoded>
			<wfw:commentRss>http://engin.bzzzt.biz/2010/07/20/zero-configuration-connectivity-on-embedded-computers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some declaration details for pointers in C/C++</title>
		<link>http://engin.bzzzt.biz/2009/12/03/some-declaration-details-for-pointers-in-cc/</link>
		<comments>http://engin.bzzzt.biz/2009/12/03/some-declaration-details-for-pointers-in-cc/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 09:13:23 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[embedded]]></category>

		<guid isPermaLink="false">http://engin.bzzzt.biz/?p=7</guid>
		<description><![CDATA[First of all let&#8217;s remember what a pointer is. A pointer is an address pointing to an object. &#8220;Address&#8221; term is open for debate, some pedantic assess would argue against it. So let&#8217;s start giving examples. const char* str ="Foo"; str[0] = 'B'; // error: assignment of read-only location str = "Bar"; // Perfectly OK]]></description>
			<content:encoded><![CDATA[<p>First of all let&#8217;s remember what a pointer is. A pointer is an address pointing to an object. &#8220;Address&#8221; term is open for debate, some pedantic assess would argue against it.</p>
<p>So let&#8217;s start giving examples.</p>
<pre>const char* str ="Foo";
str[0] = 'B'; // error: assignment of read-only location
str = "Bar"; // Perfectly OK</pre>
<p>So, it is obvious that first line declares <em>str </em>as n pointer pointing to a constant object and the object is not modifiable but the pointer itself is.</p>
<p>Let&#8217;s review another example.</p>
<pre >char * const str = "Foo";
str[0] = 'B'; // Perfectly OK (See the node below)
str = "Bar"; // error: assignment of read-only variable</pre>
<p>Here we can see that the declaration tells compiler that the pointer value (address) is constant but the object it is pointing to can be modified. Please note that even though the above assignment &#8220;str[0] = &#8216;B&#8217;&#8221; will compile just fine, it will most probably result in a segmentation fault during runtime since your compiler will most likely put it in read only memory region.</p>
<p>Now let&#8217;s make another example.</p>
<pre>const char * const str = "Foo";
str[0] = 'B'; // error: assignment of read-only location
str = "Bar"; // error: assignment of read-only variable</pre>
<p>This declaration tells the compiler that neither the object the pointer is pointing to can be modified, nor the pointer value (address) itself. i.e. nothing is modifiable :)</p>
<p><strong><span style="text-decoration: underline;">Conclusion</span></strong></p>
<p><strong>So, we can conclude that qualifiers (const, volatile) that are placed before the * applies to the object the pointer is pointing to, and qualifiers placed after the * applies to the pointer value itself, i.e. the address.</strong></p>
<p>So we can have weird looking declaration like this one:</p>
<pre>extern volatile const char * volatile const str = "Foo";</pre>
<p>I know it looks a bit unusual but it is perfectly valid.</p>
<p>Note: For hardware programming volatile keyword is a must. If you don&#8217;t know what it is, read this <a title="what is volatile keyword used for" href="http://wiki.answers.com/Q/Volatile_example_code_sample_coding_of_volatile_pointer">article</a> at wiki.answers.com first. Here&#8217;s the <a href="http://backupurl.com/ksfqr7">backup</a>, just in case.</p>
]]></content:encoded>
			<wfw:commentRss>http://engin.bzzzt.biz/2009/12/03/some-declaration-details-for-pointers-in-cc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
