Windows Forms And Threading Is Easy

Ever wanted to use threads and update the user interface without worry about the InvokeRequired thing? Use SynchronizationContext!

1 using System;
2 using System.Windows.Forms;
3 using System.Threading;
4
5 namespace WindowsFormAndThreadingIsEasy
6 {
7     public partial class Form1 : Form
8     {
9         private SynchronizationContext context;
10         private Thread myThread;
11         private event EventHandler myEvent;
12
13         public Form1()
14         {
15             InitializeComponent();
16
17             context = SynchronizationContext.Current;
18
19             myEvent += new EventHandler(Form1_myEvent);
20
21             myThread = new Thread(ThreadProc);
22             myThread.IsBackground = true;
23             myThread.Start();
24         }
25
26         private int i = 0;
27         void Form1_myEvent(object sender, EventArgs e)
28         {
29             i++;
30             textBox1.Text = i.ToString();
31         }
32
33         private void OnMyEvent(object state)
34         {
35             EventArgs e = state as EventArgs;
36             if (myEvent != null)
37                 myEvent(this, e);
38         }
39
40
41         private void ThreadProc()
42         {
43             while (true)
44             {
45                 Thread.Sleep(1000);
46
47                 if (context != null)
48                 {
49                     context.Post(new SendOrPostCallback(OnMyEvent), null);
50                 }
51                 else
52                 {
53                     OnMyEvent(null);
54                 }
55             }
56         }
57
58
59     }
60 }

Download Visual Studio 2005 Project. (unzip with 7-zip).

Atmel AVR Atmega168 RDS decoder with serial output

I have build a RDS (Radio Data System) decoder with a microcontroller from Atmel.
This decoder is fed by a RDS demodulator IC (or tuner) which has RDDA (data signal) and RDCL (clock signal) outputs.

The microcontrolller is an Atmega168 clocked on the external 4.332MHz crystal from the RDS demodulator.
In my prototype I have used the (I think obsolete) TDA7330B RDS demodulator IC from STMicroelectronics.
The TDA7330B is connected to the Atmega168 with RDDA connected to PD4 (on PORTD) and RDCL to INT0 (PD2).

The tuner I have used is an old Hauppauge WinTV PCI card with FM tuner which I can tune to the Frequency I want with some software.
The tuner module on this card is an FM1216 from Philips.
It has an unmodulated MPX FM signal output (AF sound output) which the RDS demodulator needs.
This is the same signal which is fed into a stereo decoder to produce stereo sound outputs.

The yellow wire on the right is the MPX output:

 

This is the TDA7330B RDS demodulator, you can see the MPX wire coming from the WinTV tuner.
This board also provides the clock signal for the microcontroller (wire on the crystal) and RDDA and RDCL outputs.

 

This is the Atmega168 microcontroller which tries to decode the RDDA and RDCL RDS outputs from the demodulator.
When the Atmega168 successfully decodes something it will send the output on its USART port to the PC wich is running a terminal program.
The serial output first goes to the MAX233 to convert the signal to RS232 levels. The speed is 38K4 8N1.

 

Hauppauge WinTV radio software is used to tune to the correct station:

 

The serial output from the microcontroller looks like this:

The software in the Atmega168 is able to decode the following RDS data:

  • Programme Identification code (PI: 0x83C7, Detected new station.)
  • Program service name (PS: RADIO538)
  • Programme Type code (PTY: 0x0A, Pop Music.)
  • Traffic Programme Identification code & Traffic announcement code (TP&TA: 0x01 0x00, Traffic announcements available on this station and maybe via EON on another station.)
  • Music Speech switch code (MS: 0x01, Music is being broadcasted or station does not use MS flag.)
  • Decoder-identification control code (DI: 0x01, Stereo, Dynamic PTY.)
  • Alternative frequency codes (AF: 0x98, 102.7MHz.)
  • Linkage Actuator (LA: 0x00)
  • Extended Country Code (ECC: 0xE3)
  • RadioText (RTA: Radio 538 = Randstad (Zuid) 102.7 FM)
  • Clock-time and date (CT: 0x1A53CD844, UTC 2006-07-02 (MJD 53918) 13:33:00 +02:00, TIME 15:33:00)

The software can also output all RDS groups in undecoded form by sending character ‘G’ to the Atmega:

 

Downloads

20060629-1 HEX file for the Atmage168. Note: low fuse: 0xF0, high fuse: 0xDD.

Released source code for the 20060629-1 version under GPL2 license. Have fun, let me know what you made with it! Send me some
pictures and details of your project.

20060629-1 source for the Atmage168

It is funny to note that when I am developing the software in the Atmega all sorts of radio stations have very crappy RDS encoders installed.

For example:

  • This station does not send correct UTC offset (I live in +2:00 I think):
    PI: 0x83C7, Detected new station.
    PS: RADIO538
    CT: 0x1A53CF740, UTC 2006-07-02 (MJD 53918) 15:29:00 +00:00, TIME 15:29:00

    The UTC time is send in local time.

  • However, some station can do worse:
    PI: 0x8062, Detected new station.
    RTA: Den HaagFM 92.0 Den HaagFM 92.0 Den HaagFM 92.0 Den HaagFM 92.0
    CT: 0x1A53CFD40, UTC 2006-07-02 (MJD 53918) 15:53:00 +00:00, TIME 15:53:00

    This station does not send the correct offset or the correct time, it is 10 minutes in the future.

  • This station does even worse:
    PI: 0x845A, Detected new station.
    RTA: Den Haag:89.8 MHz  Rotterdam:102.2 MHz  e-mail: studio@amorfm.nl
    CT: 0x1A53CE540, UTC 2006-07-02 (MJD 53918) 14:21:00 +00:00, TIME 14:21:00

    The time is 1,5 hours(!) in the past.

  • This station has a bug in its RDS encoder, it is toglling the Music Speech flag very fast (also the DI flag does not make sense):
    PI: 0x8062, Detected new station.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    DI: 0x04, Mono, Compressed, Dynamic PTY.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    DI: 0x00, Mono, Dynamic PTY.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    DI: 0x01, Stereo, Dynamic PTY.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    DI: 0x00, Mono, Dynamic PTY.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    MS: 0x00, Speech is being broadcasted.
    DI: 0x01, Stereo, Dynamic PTY.
    DI: 0x00, Mono, Dynamic PTY.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.
    MS: 0x00, Speech is being broadcasted.
    MS: 0x01, Music is being broadcasted or station does not use MS flag.

IPv6 without tunnel broker on linux

2 easy steps to make IPv6 work without a tunnel broker.

The technique used to make this work is called 6to4. You use a computer of somebody else to put your IPv6 traffic on the very big IPv6 network.

Okay, lets make things work:

Step 1

type this on your console to activate IPv6:
/sbin/ip tunnel add tun6to4 mode sit ttl 64 remote any local 38.107.179.237
/sbin/ip link set dev tun6to4 up
/sbin/ip -6 addr add 2002:266b:b3ed::/16 dev tun6to4
/sbin/ip -6 route add 2002::/3 via ::192.88.99.1 dev tun6to4 metric 1

Explanation: 38.107.179.237 is your ip adress from your internet provider. It is used to calculate a special part of the 2002:266b:b3ed::/16 IPv6 address as you see above. Your ip address is converted into hex and inserted as the 266b:b3ed part in this 2002: thingy.
Your IPv6 adress on the very big IPv6 network is now: 2002:266b:b3ed::

Step 2

Test your fresh IPv6 connection.
Type ping6 -nc4 www.ipv6.org in your linux console. It should say something like this:
PING www.ipv6.org(2001:6b0:1:ea:a00:20ff:fe8f:708f) 56 data bytes
64 bytes from 2001:6b0:1:ea:a00:20ff:fe8f:708f: icmp_seq=1 ttl=241 time=90.7 ms
64 bytes from 2001:6b0:1:ea:a00:20ff:fe8f:708f: icmp_seq=2 ttl=241 time=97.4 ms
64 bytes from 2001:6b0:1:ea:a00:20ff:fe8f:708f: icmp_seq=3 ttl=241 time=91.4 ms
64 bytes from 2001:6b0:1:ea:a00:20ff:fe8f:708f: icmp_seq=4 ttl=241 time=89.4 ms
Congrats if you seen no errors, you now have a working IPv6 connection. Do something usefull with it.

If you see errors.... read on, maybe I know how how to fix it.

Errors?

What to do if you get errors in any of the steps above.

Here are some things you can check:

  • You must have the iproute2 tools (check with /sbin/ip -V)
  • You must have IPv6 support in your linux kernel (check if /proc/sys/net/ipv6 exists)
  • If your public ip is not 38.107.179.237 then use the ip address on your eth0 interface of your computer (or the interface connected to the internet), take this ip and use it instead of 38.107.179.237. You do not have to recalculate the special 266b:b3ed hex part of the 2002:266b:b3ed.
  • If it still does not work, check if you have a router installed. If you do, you must configure your router to forward ip protocol 41 (NOT port 41) to your linux computer. Don't know what I'm talking about? Here is something easyer to do: configure your routers DMZ to point to your linux computer, but be sure to setup a IPv4 firewall because your linux computer is now open for the whole world.

IPv6 linux Firewall with subnet protection

Hi, this is my IPv6 firewall I use on Linux. Maybe you can do something with it:

#!/bin/bash

# DiNo, http://www.atoomnet.net/
IPTABLES="/sbin/ip6tables"

# Flush everything
echo "flush"
${IPTABLES} -F INPUT
${IPTABLES} -F OUTPUT
${IPTABLES} -F FORWARD
${IPTABLES} -F
${IPTABLES} -X extIN
${IPTABLES} -X intIN
${IPTABLES} -X extOUT
${IPTABLES} -X intOUT
${IPTABLES} -X ext2int
${IPTABLES} -X int2ext

# Default Policies
echo "policies"
${IPTABLES} -t filter -P INPUT DROP
${IPTABLES} -t filter -P OUTPUT DROP
${IPTABLES} -t filter -P FORWARD DROP

#loopback can do everything
${IPTABLES} -A INPUT   -i lo -j ACCEPT
${IPTABLES} -A FORWARD -i lo -j ACCEPT
${IPTABLES} -A OUTPUT  -i lo -j ACCEPT

# chain of all public incoming ipv6 interfaces
echo "extIN"
${IPTABLES} -N extIN
${IPTABLES} -A INPUT -i sixxs   -j extIN
${IPTABLES} -A INPUT -i tun6to4 -j extIN

# chain of all public outgoing ipv6 interfaces
echo "extOUT"
${IPTABLES} -N extOUT
${IPTABLES} -A OUTPUT -o sixxs   -j extOUT
${IPTABLES} -A OUTPUT -o tun6to4 -j extOUT

# chain of all internal incoming ipv6 interfaces
echo "intIN"
${IPTABLES} -N intIN
${IPTABLES} -A INPUT -i bridge0  -j intIN
${IPTABLES} -A INPUT -i atoomnet -j intIN
${IPTABLES} -A INPUT -i tap0     -j intIN

# chain of all internal outgoing ipv6 interfaces
echo "intOUT"
${IPTABLES} -N intOUT
${IPTABLES} -A OUTPUT -o bridge0  -j intOUT
${IPTABLES} -A OUTPUT -o atoomnet -j intOUT
${IPTABLES} -A OUTPUT -o tap0     -j intOUT

# chain of external to internal forward interfaces
echo "ext2int"
${IPTABLES} -N ext2int
${IPTABLES} -A FORWARD -i sixxs -j ext2int
${IPTABLES} -A FORWARD -i tun6to4 -j ext2int

# chain of internal to external forward interfaces
echo "int2ext"
${IPTABLES} -N int2ext
${IPTABLES} -A FORWARD -i bridge0 -j int2ext

#logging
${IPTABLES} -A INPUT   -m limit --limit 10/minute -j LOG --log-prefix "INPUT_DROP:"
${IPTABLES} -A OUTPUT  -m limit --limit 10/minute -j LOG --log-prefix "OUTPUT_DROP:"
${IPTABLES} -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "FORWARD_DROP:"

#Tha Rulez...

# allow all internal hosts to this server
echo "intIN rules"
${IPTABLES} -A intIN -j ACCEPT

# allow outgoing traffic to internal hosts
echo "intOUT rules"
${IPTABLES} -A intOUT -j ACCEPT

# allow outgoing traffic to external hosts
echo "extOUT rules"
${IPTABLES} -A extOUT -j ACCEPT

# allow incoming traffic
echo "extIN rules"
${IPTABLES} -A extIN -p tcp --dport 22 -j ACCEPT
${IPTABLES} -A extIN -p tcp --dport 80 -j ACCEPT
${IPTABLES} -A extIN -p tcp --dport 25 -j ACCEPT
${IPTABLES} -A extIN -p udp --dport 53 -j ACCEPT
${IPTABLES} -A extIN -p tcp --dport 53 -j ACCEPT
${IPTABLES} -A extIN -p tcp ! --syn -j ACCEPT
${IPTABLES} -A extIN -p icmpv6 -j ACCEPT --match limit --limit 30/minute
${IPTABLES} -A extIN -m limit --limit 10/minute -j LOG --log-prefix "extIN_DROP:"
${IPTABLES} -A extIN -j DROP

# allow all internal hosts to go play outside
echo "int2ext rules"
${IPTABLES} -A int2ext -j ACCEPT

# allow all external hosts to go inside
echo "ext2int rules"
${IPTABLES} -A ext2int -p tcp ! --syn -j ACCEPT
${IPTABLES} -A ext2int -p icmpv6 -j ACCEPT --match limit --limit 30/minute
${IPTABLES} -A ext2int -m limit --limit 10/minute -j LOG --log-prefix "ext2int_DROP:"
${IPTABLES} -A ext2int -j DROP