Thursday, December 15, 2016

Python 3 a few network scripts

Get netmask

import socket
import fcntl
import struct

iface='eth0'
socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack('256s', iface.encode('utf-8')))[20:24])

A broadcast server
from socket import *
import time

s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
while True:
    s.sendto('this is testing'.encode('utf-8'), ('255.255.255.255', 12345))
    time.sleep(1)



A broadcast listener
from socket import *
s=socket(AF_INET, SOCK_DGRAM)
s.bind(('', 12345))
while True:
    m=s.recvfrom(1024)
    print(m[0])

Monday, September 26, 2016

FUSE sshfs

Installation
sudo apt-get install fuse
sudo apt-get install sshfs
sudo apt-get install exfat-fuse exfat-utils
Create path
mkdir sshfs-path

Mount
sshfs root@172.16.40.83:/run /home/fcm/sshfs-path/
Unmount
fusermount -u /home/fcm/sshfs-path

Sunday, September 25, 2016

python 3 compliance pyzmq sample

These two python script files are modified from pyzmq samples

topics_pub.py

#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.

Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost.  More than
one subscriber can listen, and they can listen to  different topics.

Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks'  while listening to 'w' is enough to
catch 'weather'.
"""

#-----------------------------------------------------------------------------
#  Copyright (c) 2010 Brian Granger
#
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------

import itertools
import sys
import time

import zmq

def main():
    if len (sys.argv) != 2:
        print('usage: publisher <bind-to>')
        sys.exit (1)

    bind_to = sys.argv[1]

    all_topics = ['sports.general','sports.football','sports.basketball',
                  'stocks.general','stocks.GOOG','stocks.AAPL',
                  'weather']

    ctx = zmq.Context()
    s = ctx.socket(zmq.PUB)
    s.bind(bind_to)

    print("Starting broadcast on topics:")
    print("   %s" % all_topics)
    print("Hit Ctrl-C to stop broadcasting.")
    print("Waiting so subscriber sockets can connect...")
    print()
    time.sleep(1.0)

    msg_counter = itertools.count()
    try:
        for topic in itertools.cycle(all_topics):
            msg_body = str(next(msg_counter))
            print("    Topic: {}, msg:{}".format(topic, msg_body))
            s.send_multipart([topic.encode(), msg_body.encode()])
            # short wait so we don't hog the cpu
            time.sleep(0.1)
    except KeyboardInterrupt:
        pass

    print("Waiting for message queues to flush...")
    time.sleep(0.5)
    print("Done.")

if __name__ == "__main__":
    main()


topics_sub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.

Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost.  More than
one subscriber can listen, and they can listen to  different topics.

Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks'  while listening to 'w' is enough to
catch 'weather'.
"""

#-----------------------------------------------------------------------------
#  Copyright (c) 2010 Brian Granger, Fernando Perez
#
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------

import sys
import time

import zmq
import numpy

def main():
    if len (sys.argv) < 2:
        print ('usage: subscriber <connect_to> [topic topic ...]')
        sys.exit (1)

    connect_to = sys.argv[1]
    topics = sys.argv[2:]

    ctx = zmq.Context()
    s = ctx.socket(zmq.SUB)
    print(connect_to)
    s.connect(connect_to)

    # manage subscriptions
    if not topics:
        print("Receiving messages on ALL topics...")
        s.setsockopt(zmq.SUBSCRIBE,'')
    else:
        print("Receiving messages on topics: %s ..." % topics)
        for t in topics:
            s.setsockopt_string(zmq.SUBSCRIBE,t)
    print()
    try:
        while True:
            topic, msg = s.recv_multipart()
            print('   Topic: %s, msg:%s' % (topic, msg))
    except KeyboardInterrupt:
        pass
    print("Done.")

if __name__ == "__main__":
    main()


raspberry pi (mac os x)

Not yet there.

http://www.jaredwolff.com/blog/cross-compiling-on-mac-osx-for-raspberry-pi/

git:
https://github.com/raspberrypi

buildroot sample
https://github.com/gamaral/rpi-buildroot/

PiLFS
http://www.intestinate.com/pilfs/
http://www.intestinate.com/pilfs/guide.html

GPIO

gpio.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>

int irq_number;

static irqreturn_t gpio_interrupt_handler(int irq, void* dev_id) {
        printk(KERN_ERR "gpio0 IRQ %d event",irq_number);
        return(IRQ_HANDLED);
}


static int __init gpio_init(void) {
        irq_number = gpio_to_irq(25);

        if ( request_irq(irq_number, gpio_interrupt_handler, IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING|IRQF_ONESHOT, "gpio_reset", NULL) ) {
                printk(KERN_ERR "GPIO_RESET: trouble requesting IRQ %d",irq_number);
                return(-EIO);
        } else {
                printk(KERN_ERR "GPIO_RESET: requesting IRQ %d-> fine\n",irq_number);
        }

        return 0;
}

static void __exit gpio_exit(void) {
        free_irq(irq_number, NULL);
        printk ("gpio_reset module unloaded\n");
        return;
}

module_init(gpio_init);
module_exit(gpio_exit);

MODULE_LICENSE("GPL");

Makefile

CCPREFIX=arm-linux-gnueabihf-

obj-m += gpio.o

all:
 make ARCH=arm CROSS_COMPILE=${CCPREFIX} -C /home/fcm/rpi/rpi-buildroot/output/build/linux-rpi-4.4.y M=$(PWD) modules
clean:
 rm *.ko
 rm *.o
 rm *.mod*

Thursday, September 8, 2016

Unity 3D on iOS

Doc:
https://docs.unity3d.com/Manual/iphone-GettingStarted.html

Part 1:
https://www.raywenderlich.com/25205/beginning-unity-3d-for-ios-part-13
Part 2:
https://www.raywenderlich.com/?p=25349
Part 3:
https://www.raywenderlich.com/?p=25828

Submission:
https://unity3d.com/learn/tutorials/topics/mobile-touch/how-submit-ios-app-store-overview

Bezier Curve

Quadratic:

Cubic:



In case of the cubic formula P0, P1 and P2 are your control points. t has to be a value between 0 and 1 and represents the "position" on the curve. By incrementing t step by step you'll get several points you can use to actually draw the curve.

So using the above formula you could do something like
glBegin(GL_LINE_STRIP);
for(float t=0; t <= 1; t += 0.1) {
     float x = (1-t)*(1-t)*p0.x + 2(1-t)*t*p1.x + t*t*p2.x;
     float y = (1-t)*(1-t)*p0.y + 2(1-t)*t*p1.y + t*t*p2.y;
     float z = (1-t)*(1-t)*p0.z + 2(1-t)*t*p1.z + t*t*p2.z;

     glVertex3f(x, y, z);
}
glEnd();
The smaller the steps you use for t the smoother the curve will become. That's it. Really.
Generic:
http://www.gamedev.net/topic/534082-drawing-a-curve-in-opengles---how/

iOS:
http://stackoverflow.com/questions/5054790/cgpathref-bezier-curves-in-opengl-es

Android:
http://blog.uncle.se/2012/02/opengl-es-tutorial-for-android-part-ii-building-a-polygon/

Unity:
http://www.theappguruz.com/blog/bezier-curve-in-games

Wednesday, September 7, 2016

github access from ubuntu

sudo apt-get install git
sudo apt-get install ssh
export SSH_AUTH_SOCK=0
eval `ssh-agent -s`
ssh-keygen -t rsa
ssh-add ~/.ssh/id_rsa
ssh-add -l
cat ~/.ssh/id_rsa.pub
go to github and add public key

ssh -vT git@github.com
"Hi XXX! You've successfully authenticated, but GitHub does notprovide shell access"

git clone git@github.com:xxx/xxxx.git
cd xxxx/
git add xxxx.py
git add xxxx.*
git remote -v
git remote remove origin
git remote add origin git@github.com:xxx/xxxx.git
git push --set-upstream origin master
git branch
git push

Friday, August 26, 2016

new frontier: infographics

http://www.huffingtonpost.com/nadya-khoja/6-top-infographic-design-_b_11707120.html

https://en.wikipedia.org/wiki/Infographic

netstat No -p option

Get inode and port relationship
#cat /proc/net/tcp
  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                         
   0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 2495 1 ce037b40 100 0 0 10 -1                 
   1: 0100007F:177A 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 2807 1 ce036940 100 0 0 10 -1                 
   2: 0100007F:177B 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 3244 1 ce036dc0 100 0 0 10 -1                 
   3: 652910AC:0016 A72810AC:CFF8 01 00000034:00000000 01:00000026 00000000     0        0 2793 3 ce036040 39 4 11 10 -1                 
   4: 652910AC:0016 A72810AC:CFF9 01 00000000:00000000 00:00000000 00000000     0        0 2801 1 ce0364c0 24 4 25 10 -1                 
   5: 652910AC:0016 A72810AC:D03A 01 00000000:00000000 00:00000000 00000000     0        0 3239 1 ce037240 24 4 25 10 -1                 
   6: 652910AC:0016 A72810AC:D039 01 00000000:00000000 00:00000000 00000000     0        0 3231 1 ce0376c0 25 4 1 10 -1                  

Find out inode and process relationship
#find /proc/*/fd | xargs ls -l | grep socket | grep proc
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1040/fd/4 -> socket:[2258]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1040/fd/7 -> socket:[2261]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1040/fd/8 -> socket:[2262]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1049/fd/3 -> socket:[2300]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1052/fd/3 -> socket:[2303]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1100/fd/10 -> socket:[2354]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1100/fd/9 -> socket:[2024]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1101/fd/10 -> socket:[2355]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1101/fd/9 -> socket:[2024]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1258/fd/3 -> socket:[2490]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1258/fd/4 -> socket:[2495]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1352/fd/5 -> socket:[2708]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1399/fd/3 -> socket:[2490]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1399/fd/5 -> socket:[2793]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1399/fd/7 -> socket:[2807]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1400/fd/3 -> socket:[2490]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/1400/fd/5 -> socket:[2801]
lrwx------    1 root     root            64 Aug 26 12:49 /proc/1575/fd/3 -> socket:[2490]
lrwx------    1 root     root            64 Aug 26 12:49 /proc/1575/fd/5 -> socket:[3231]
lrwx------    1 root     root            64 Aug 26 12:49 /proc/1575/fd/7 -> socket:[3244]
lrwx------    1 root     root            64 Aug 26 12:49 /proc/1576/fd/3 -> socket:[2490]
lrwx------    1 root     root            64 Aug 26 12:49 /proc/1576/fd/5 -> socket:[3239]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/931/fd/4 -> socket:[1994]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/931/fd/5 -> socket:[1995]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/931/fd/8 -> socket:[2023]
lrwx------    1 root     root            64 Aug 26 12:44 /proc/931/fd/9 -> socket:[2024]

Find out PID of a process
#ps -axl | grep 1400
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
1     0  1400  1258  20   0   2440  1208 poll_s Ss   ?          0:00 dropbear 

Monday, August 22, 2016

Quick note on zeromq cross complie and deployment

./configure --host=arm-linux-gnueabihf --prefix=/home/xxx/temp/arm

export LDFLAGS="-L/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/lib -L/home/xxx/temp/arm/lib"
export CPPFLAGS="-I/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/arm-linux-gnueabihf/include/c++/4.7.3/"



add path to cross compiler
export PATH=$PATH:/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/bin


$ make
$ make install



at this moment, the libzmq is available at /home/xxx/temp/arm/lib

$ arm-linux-gnueabihf-gcc $LDFLAGS $CPPFLAGS -lzmq wuclient.c -o wuclient
$ cat wuclient.c
//  Weather update client
//  Connects SUB socket to tcp://localhost:5556
//  Collects weather updates and finds avg temp in zipcode

#include "zhelpers.h"

int main (int argc, char *argv [])
{
    //  Socket to talk to server
    printf ("Collecting updates from weather server...\n");
    void *context = zmq_ctx_new ();
    void *subscriber = zmq_socket (context, ZMQ_SUB);
    int rc = zmq_connect (subscriber, "tcp://127.0.0.1:5556");
    assert (rc == 0);

    //  Subscribe to zipcode, default is NYC, 10001
    char *filter = (argc > 1)? argv [1]: "10001 ";
    rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
                         filter, strlen (filter));
    assert (rc == 0);

    //  Process 100 updates
    int update_nbr;
    long total_temp = 0;
    for (update_nbr = 0; update_nbr < 100; update_nbr++) {
        char *string = s_recv (subscriber);
        printf("%s\n", string);
        int zipcode, temperature, relhumidity;
        sscanf (string, "%d %d %d",
            &zipcode, &temperature, &relhumidity);
        total_temp += temperature;
        free (string);
    }
    printf ("Average temperature for zipcode '%s' was %dF\n",
        filter, (int) (total_temp / update_nbr));

    zmq_close (subscriber);
    zmq_ctx_destroy (context);
    return 0;
}

$ arm-linux-gnueabihf-gcc $LDFLAGS $CPPFLAGS -lzmq wuserver.c -o wuserver
$ cat wuserver.c
//  Weather update server
//  Binds PUB socket to tcp://*:5556
//  Publishes random weather updates

#include "zhelpers.h"

int main (void)
{
    //  Prepare our context and publisher
    void *context = zmq_ctx_new ();
    void *publisher = zmq_socket (context, ZMQ_PUB);
    int rc = zmq_bind (publisher, "tcp://lo:5556");
    assert (rc == 0);

    //  Initialize random number generator
    srandom ((unsigned) time (NULL));
    while (1) {
        //  Get values that will fool the boss
        int zipcode, temperature, relhumidity;
        zipcode     = randof (100000);
        temperature = randof (215) - 80;
        relhumidity = randof (50) + 10;

        //  Send message to all subscribers
        char update [20];
        sprintf (update, "%05d %d %d", zipcode, temperature, relhumidity);
        s_send (publisher, update);
    }
    zmq_close (publisher);
    zmq_ctx_destroy (context);
    return 0;
}

$ cat zhelpers.h
/*  =====================================================================
    zhelpers.h

    Helper header file for example applications.
    =====================================================================
*/

#ifndef __ZHELPERS_H_INCLUDED__
#define __ZHELPERS_H_INCLUDED__

//  Include a bunch of headers that we will need in the examples

#include <zmq.h>

#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if (!defined (WIN32))
#   include <sys/time.h>
#endif

#if (defined (WIN32))
#   include <windows.h>
#endif

//  Version checking, and patch up missing constants to match 2.1
#if ZMQ_VERSION_MAJOR == 2
#   error "Please upgrade to ZeroMQ/3.2 for these examples"
#endif

//  On some version of Windows, POSIX subsystem is not installed by default.
//  So define srandom and random ourself.
#if (defined (WIN32))
#   define srandom srand
#   define random rand
#endif

//  Provide random number from 0..(num-1)
#define randof(num)  (int) ((float) (num) * random () / (RAND_MAX + 1.0))

//  Receive 0MQ string from socket and convert into C string
//  Caller must free returned string. Returns NULL if the context
//  is being terminated.
static char *
s_recv (void *socket) {
    char buffer [256];
    int size = zmq_recv (socket, buffer, 255, 0);
    if (size == -1)
        return NULL;
    return strndup (buffer, sizeof(buffer) - 1);
    // remember that the strdup family of functions use malloc/alloc for space for the new string.  It must be manually
    // freed when you are done with it.  Failure to do so will allow a heap attack.
}

//  Convert C string to 0MQ string and send to socket
static int
s_send (void *socket, char *string) {
    int size = zmq_send (socket, string, strlen (string), 0);
    return size;
}

//  Sends string as 0MQ string, as multipart non-terminal
static int
s_sendmore (void *socket, char *string) {
    int size = zmq_send (socket, string, strlen (string), ZMQ_SNDMORE);
    return size;
}

//  Receives all message parts from socket, prints neatly
//
static void
s_dump (void *socket)
{
    int rc;

    zmq_msg_t message;
    rc = zmq_msg_init (&message);
    assert (rc == 0);

    puts ("----------------------------------------");
    //  Process all parts of the message
    do {
        int size = zmq_msg_recv (&message, socket, 0);
        assert (size >= 0);

        //  Dump the message as text or binary
        char *data = (char*)zmq_msg_data (&message);
        assert (data != 0);
        int is_text = 1;
        int char_nbr;
        for (char_nbr = 0; char_nbr < size; char_nbr++) {
            if ((unsigned char) data [char_nbr] < 32
                || (unsigned char) data [char_nbr] > 126) {
                is_text = 0;
            }
        }

        printf ("[%03d] ", size);
        for (char_nbr = 0; char_nbr < size; char_nbr++) {
            if (is_text) {
                printf ("%c", data [char_nbr]);
            } else {
                printf ("%02X", (unsigned char) data [char_nbr]);
            }
        }
        printf ("\n");
    } while (zmq_msg_more (&message));

    rc = zmq_msg_close (&message);
    assert (rc == 0);
}

#if (!defined (WIN32))
//  Set simple random printable identity on socket
//  Caution:
//    DO NOT call this version of s_set_id from multiple threads on MS Windows
//    since s_set_id will call rand() on MS Windows. rand(), however, is not
//    reentrant or thread-safe. See issue #521.
static void
s_set_id (void *socket)
{
    char identity [10];
    sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
    zmq_setsockopt (socket, ZMQ_IDENTITY, identity, strlen (identity));
}
#else
//  Fix #521 for MS Windows.
static void
s_set_id(void *socket, intptr_t id)
{
    char identity [10];
    sprintf(identity, "%04X", (int)id);
    zmq_setsockopt(socket, ZMQ_IDENTITY, identity, strlen(identity));
}
#endif

//  Sleep for a number of milliseconds
static void
s_sleep (int msecs)
{
#if (defined (WIN32))
    Sleep (msecs);
#else
    struct timespec t;
    t.tv_sec  =  msecs / 1000;
    t.tv_nsec = (msecs % 1000) * 1000000;
    nanosleep (&t, NULL);
#endif
}

//  Return current system clock as milliseconds
static int64_t
s_clock (void)
{
#if (defined (WIN32))
    SYSTEMTIME st;
    GetSystemTime (&st);
    return (int64_t) st.wSecond * 1000 + st.wMilliseconds;
#else
    struct timeval tv;
    gettimeofday (&tv, NULL);
    return (int64_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000);
#endif
}

//  Print formatted string to stdout, prefixed by date/time and
//  terminated with a newline.

static void
s_console (const char *format, ...)
{
    time_t curtime = time (NULL);
    struct tm *loctime = localtime (&curtime);
    char *formatted = (char*)malloc (20);
    strftime (formatted, 20, "%y-%m-%d %H:%M:%S ", loctime);
    printf ("%s", formatted);
    free (formatted);

    va_list argptr;
    va_start (argptr, format);
    vprintf (format, argptr);
    va_end (argptr);
    printf ("\n");
}

#endif  //  __ZHELPERS_H_INCLUDED__


package wuclient wuserver and /home/xxx/temp/arm/*
deploy them to an arm target

after extract everything, 
export LD_LIBRARY_PATH=/xxx/lib

wuserver
wuclient

a simple timer for linux applications

/*
 * setitimer.c - simple use of the interval timer
 */

#include <sys/time.h>        /* for setitimer */
#include <unistd.h>        /* for pause */
#include <signal.h>        /* for signal */

#define INTERVAL 500        /* number of milliseconds to go off */

/* function prototype */
void DoStuff(void);

int main(int argc, char *argv[]) {

  struct itimerval it_val;    /* for setting itimer */

  /* Upon SIGALRM, call DoStuff().
   * Set interval timer.  We want frequency in ms,
   * but the setitimer call needs seconds and useconds. */
  if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) {
    perror("Unable to catch SIGALRM");
    exit(1);
  }
  it_val.it_value.tv_sec =     INTERVAL/1000;
  it_val.it_value.tv_usec =    (INTERVAL*1000) % 1000000;   
  it_val.it_interval = it_val.it_value;
  if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
    perror("error calling setitimer()");
    exit(1);
  }

  while (1)
    pause();

}

/*
 * DoStuff
 */
void DoStuff(void) {

  printf("Timer went off.\n");

}

Tuesday, August 16, 2016

pyzmq 15.4 and python 3.5.2

It takes a little time to get the latest pyzmq installed.  So far, here is a list of things that I have done.
Note: This is on Ubuntu 16.04.

Preparation
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo python3 -m pip install cython

Download zeromq 4.1.5
http://zeromq.org/intro:get-the-software

Build zeromq 4.1.5
tar xf zeromq-4.1.5.tar.gzcd zeromq-4.1.5
./configure
make
sudo make install

Download pyzmq 15.4
https://github.com/zeromq/pyzmq

unzip pyzmq.zip

python3 setup.py configure --zmq=/usr/local

sudo python3 setup.py install

===============================
pub sub sample

python3 topics_sub.py "tcp://127.0.0.1:5556"
python3 topics_pub.py "tcp://127.0.0.1:5556" ""
===============================
topics_pub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.

Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost.  More than
one subscriber can listen, and they can listen to  different topics.

Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks'  while listening to 'w' is enough to
catch 'weather'.
"""

#-----------------------------------------------------------------------------
#  Copyright (c) 2010 Brian Granger
#
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------

import itertools
import sys
import time

import zmq

def main():
    if len (sys.argv) != 2:
        print('usage: publisher <bind-to>')
        sys.exit (1)

    bind_to = sys.argv[1]

    all_topics = ['sports.general','sports.football','sports.basketball',
                  'stocks.general','stocks.GOOG','stocks.AAPL',
                  'weather']

    ctx = zmq.Context()
    s = ctx.socket(zmq.PUB)
    s.bind(bind_to)

    print("Starting broadcast on topics:")
    print("   %s" % all_topics)
    print("Hit Ctrl-C to stop broadcasting.")
    print("Waiting so subscriber sockets can connect...")
    print()
    time.sleep(1.0)

    msg_counter = itertools.count()
    try:
        for topic in itertools.cycle(all_topics):
            msg_body = str(next(msg_counter))
            print("    Topic: {}, msg:{}".format(topic, msg_body))
            s.send_multipart([topic.encode(), msg_body.encode()])
            # short wait so we don't hog the cpu
            time.sleep(0.1)
    except KeyboardInterrupt:
        pass

    print("Waiting for message queues to flush...")
    time.sleep(0.5)
    print("Done.")

if __name__ == "__main__":
    main()


topics_sub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.

Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost.  More than
one subscriber can listen, and they can listen to  different topics.

Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks'  while listening to 'w' is enough to
catch 'weather'.
"""

#-----------------------------------------------------------------------------
#  Copyright (c) 2010 Brian Granger, Fernando Perez
#
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------

import sys
import time

import zmq
import numpy

def main():
    if len (sys.argv) < 2:
        print ('usage: subscriber <connect_to> [topic topic ...]')
        sys.exit (1)

    connect_to = sys.argv[1]
    topics = sys.argv[2:]

    ctx = zmq.Context()
    s = ctx.socket(zmq.SUB)
    print(connect_to)
    s.connect(connect_to)

    # manage subscriptions
    if not topics:
        print("Receiving messages on ALL topics...")
        s.setsockopt(zmq.SUBSCRIBE,'')
    else:
        print("Receiving messages on topics: %s ..." % topics)
        for t in topics:
            s.setsockopt_string(zmq.SUBSCRIBE,t)
    print()
    try:
        while True:
            topic, msg = s.recv_multipart()
            print('   Topic: %s, msg:%s' % (topic, msg))
    except KeyboardInterrupt:
        pass
    print("Done.")

if __name__ == "__main__":
    main()




Sunday, August 7, 2016

z50-70 hackintosh note

https://goo.gl/2bpXvt

Pretty much following the following two threads.
http://www.tonymacx86.com/threads/guide-lenovo-z50-70-using-clover-uefi-10-11.179520/
http://www.tonymacx86.com/threads/guide-booting-the-os-x-installer-on-laptops-with-clover.148093/

Note:
1) Clover version 3625 is good.  Other versions may not boot.  This is the main lesson learned.
2) Do not add more than needed. 
3) Use MBR

Partition USB
diskutil list
diskutil partitionDisk /dev/disk1 2 MBR FAT32 "CLOVER EFI" 200Mi HFS+J "install_osx" R

Use Clover EFI 3625 install to install_osx

createinstallmedia to install_osx
https://support.apple.com/en-us/HT201372

BIOS enable UEFI first

Install and enjoy

Issues:
-WiFi
-Audio

Thursday, July 28, 2016

How to design Business Intelligence?


http://www.thereformedprogrammer.net/architecture-of-business-layer-working-with-entity-framework/

https://www.simple-talk.com/dotnet/asp.net/using-entity-framework-with-an-existing-database--user-interface/

http://dddcommunity.org/learning-ddd/what_is_ddd/


IoT connectivity

http://resources.alcatel-lucent.com/asset/200178

Monday, July 25, 2016

pybee python ios

https://github.com/pybee/Python-Apple-support

#import "Python/Python.h"

http://stackoverflow.com/questions/9749260/python-for-ios-interpreter

A little trip to Ubuntu 16.04
Install Python 3.5
1
sudo apt-get install python3 python3-dev

helloworld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <Python.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    Py_Finalize();
    PyMem_RawFree(program);
    return 0;
}

build
1
3
gcc -c hellowworld.c
gcc helloworld.o $(/usr/bin/python3.5-config --ldflags)


Calling Python from C and fetching return values

Python: sample.py
1
2
3
# Returns the sum of two numbers.
def add(a, b):
 return a+b

sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <python3.5/Python.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
 char localPath[40] = "\/home\/XXX\/XXX\/";

 printf("Calling Python to find the sum of 2 and 2.\n");

 // Initialize the Python interpreter.
 Py_Initialize();

 // Create some Python objects that will later be assigned values.
 PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

 // Convert the file name to a Python string.
 pName = PyUnicode_FromString("sample");

 PyObject *sys_path = PySys_GetObject("path");
 PyList_Append(sys_path, PyUnicode_FromString(localPath));

 // Import the file as a Python module.
 pModule = PyImport_Import(pName);

 // Create a dictionary for the contents of the module.
 pDict = PyModule_GetDict(pModule);

 // Get the add method from the dictionary.
 pFunc = PyDict_GetItemString(pDict, "add");

 // Create a Python tuple to hold the arguments to the method.
 pArgs = PyTuple_New(2);

 // Convert 2 to a Python integer.
 pValue = PyLong_FromLong(2);

 // Set the Python int as the first and second arguments to the method.
 PyTuple_SetItem(pArgs, 0, pValue); 
 PyTuple_SetItem(pArgs, 1, pValue); 

 // Call the function with the arguments.
 PyObject* pResult = PyObject_CallObject(pFunc, pArgs);

 // Print a message if calling the method failed.
 if(pResult == NULL) printf("Calling the add method failed.\n");

 // Convert the result to a long from a Python object.
 long result = PyLong_AsLong(pResult);

 // Destroy the Python interpreter.
 Py_Finalize();

 // Print the result.
 printf("The result is %d.\n", result);
 //std::cin.ignore();

 return 0;
}

Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- (void)helloWorld
{
    printf("Calling Python to find the sum of 2 and 2.\n");
    
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    PyObject *sys_path = PySys_GetObject("path");
    PyList_Append(sys_path, PyUnicode_FromString([resourcePath UTF8String]));

    // Special environment to avoid writing bytecode because
    // the process will not have write attribute on the device.
    putenv("PYTHONDONTWRITEBYTECODE=1");
    
    NSString *python_home = [NSString stringWithFormat:@"%@/Library/Python.framework/Resources", resourcePath, nil];
    NSLog(@"PythonHome is: %@", python_home);
    wchar_t *wpython_home = Py_DecodeLocale([python_home UTF8String], NULL);
    Py_SetPythonHome(wpython_home);
    
    // iOS provides a specific directory for temp files.
    NSString *tmp_path = [NSString stringWithFormat:@"TMP=%@/tmp", resourcePath, nil];
    putenv((char *)[tmp_path UTF8String]);
    
    // Initialize the Python interpreter.
    Py_Initialize();
    
    // Create some Python objects that will later be assigned values.
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
    
    // Convert the file name to a Python string.
    pName = PyUnicode_FromString("sample");
    
    // Import the file as a Python module.
    pModule = PyImport_Import(pName);
    
    // Create a dictionary for the contents of the module.
    pDict = PyModule_GetDict(pModule);
    
    // Get the add method from the dictionary.
    pFunc = PyDict_GetItemString(pDict, "add");
    
    // Create a Python tuple to hold the arguments to the method.
    pArgs = PyTuple_New(2);
    
    // Convert 2 to a Python integer.
    pValue = PyLong_FromLong(2);
    
    // Set the Python int as the first and second arguments to the method.
    PyTuple_SetItem(pArgs, 0, pValue);
    PyTuple_SetItem(pArgs, 1, pValue);
    
    // Call the function with the arguments.
    PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
    
    // Print a message if calling the method failed.
    if(pResult == NULL) printf("Calling the add method failed.\n");
    
    // Convert the result to a long from a Python object.
    long result = PyLong_AsLong(pResult);
    
    // Destroy the Python interpreter.
    Py_Finalize();
    
    // Print the result.
    printf("The result is %d.\n", result);
}

Sunday, July 10, 2016

xcode 7 and git

Set up git server on Ubuntu
http://5techy.blogspot.com/2015/10/set-up-git-server-on-ubuntu.html

prepare git server for a repo

1
2
3
mkdir /opt/git/project.git
cd /opt/git/project.git
git init

put initial version in
1
2
3
4
5
6
7
touch README
git init
git add .
git commit -m "first commit"
git remote add origin ssh://git@gitServer:/opt/git/project.git
git remote -v
git push origin master

on Mac OS X
1
2
cd $project
rm -rf .git

start Xcode, open project

Source Control -> Create Working Copy

Source Control -> "project" -> Configure "Project"
remote tab, "+", "ssh://git@gitServer:/opt/git/project.git"
done

Source Control -> "project" -> New Branch, "workingBranch"

Source Control -> Push -> origin/workingBranch

Now, source code are on the git server.



Monday, June 27, 2016

RegExr.com

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

Tuesday, June 21, 2016

some band info

GSM 850 DL: 869.00-894.00
GSM 1900 DL: 1930.00-1990.00
LTE 700 DL: 746.00-755.90, 758.00 - 663.00

Monday, June 13, 2016

mixing c++ and obj-c / ndk

mixing c++ and ndk
https://www.sitepoint.com/using-c-and-c-code-in-an-android-app-with-the-ndk/


mixing c++ and obj-c
https://www.sitepoint.com/using-c-and-c-in-an-ios-app-with-objective-c/

http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++

Mixing Objective-C, C++ and Objective-C++: an Updated Summary

Originally published on 25th May 2012, updated on 15th July 2012.
Quite some time ago, I ran into the situation of including a C++ library in an Objective-C project. I failed to find any sensible documentation on the subject, so I came up with a solution myself and eventually wrote it up in an article. That article went on to become something of a sleeper hit (by my modest standards anyway) and is to this day one of the highest-ranked results for Objective-C++ and related keywords on Google.
Since then, Apple has switched to the LLVM-based clang as the primary compiler for Mac and iOS development. One of the effects of this has been an accelerated pace of changes to the Objective-C language, compared to the rather more glacial rate of change under the GCC regime. One particular change has caused my old article to no longer be up-to-date. This, along with the steady stream of clarification questions I receive about it, has prompted me to write this new article.

Recap of the problem

To save you going through the old article, here's the issue: let's say you have some existing C++ code, a library perhaps, and you want to use it in an Objective-C application. Typically, your C++ code will define some class you'd like to use. You could switch your whole project to Objective-C++ by renaming all the .m files to .mm, and freely mix C++ and Objective-C. That's certainly an option, but the two worlds are quite different, so such "deep" mixing can become awkward.
So usually you'll want to wrap the C++ types and functions with Objective-C equivalents that you can use in the rest of your project. Let's say you have a C++ class called CppObject, defined in CppObject.h:
#include <string>
class CppObject
{
public:
  void ExampleMethod(const std::string& str);
  // constructor, destructor, other members, etc.
};
You can have C++-typed members in an Objective-C class, so the typical first attempt is to do this with your wrapper class, ObjcObject - in ObjcObject.h:
#import <Foundation/Foundation.h>
#import "CppObject.h"

@interface ObjcObject : NSObject {
  CppObject wrapped;
}
- (void)exampleMethodWithString:(NSString*)str;
// other wrapped methods and properties
@end
And then implementing the methods in ObjcObject.mm. Many are then surprised to get preprocessor and compile errors in ObjcObject.h and CppObject.h when they #import "ObjcObject.h" from a pure Objective-C (.m) file directly or indirectly via another header (.h) file. The thing to bear in mind is that the preprocessor basically just does text substitution, so #include and #import directives are essentially equivalent to recursively copy-and-pasting the contents of the file in question into the location of the directive. So in this example, if you #import "ObjcObject.h" you're essentially inserting the following code:
// [lots and lots of Objective-C code from Foundation/Foundation.h]
// [fail to include <string>] as that header is not in the include path outside of C++ mode
class CppObject
{
public:
  void ExampleMethod(const std::string& str);
  // constructor, destructor, other members, etc.
};

@interface ObjcObject : NSObject {
  CppObject wrapped;
}
- (void)exampleMethodWithString:(NSString*)str;
// other wrapped methods and properties
@end
The compiler will get enormously confused by class CppObject and the block following it, as that's simply not valid Objective-C syntax. The error will typically be something like
Unknown type name 'class'; did you mean 'Class'?
as there is no class keyword in Objective-C. So to be compatible with Objective-C, our Objective-C++ class's header file must contain only Objective-C code, absolutely no C++ - this mainly affects types in particular (like the CppObject class type here).

Keeping your headers clean

In the old article, I talked through a few solutions to this, so I won't reiterate them here. The nicest one at the time was the PIMPL idiom. It continues to work well today, and is still the best way for the opposite problem of wrapping Objective-C with C++ (more on that later on). However, with clang, there is a new way to keep C++ out of your Objective-C headers: ivars in class extensions.
Class extensions (not to be confused with categories) have existed in Objective-C for a while: they let you declare additional parts of the class's interface outside the public header before the @implementation block. As such, the only sensible place to put them is just above said block, e.g. ObjcObject.mm:
#import "ObjcObject.h"
@interface ObjcObject () // note the empty parentheses
- (void)methodWeDontWantInTheHeaderFile;
@end
@implementation ObjcObject
// etc.
This much already worked with GCC, but with clang, you can also add an ivar block to it. This means we can declare any instance variables with C++ types in the extension, or at the start of the @implementation block. In our case, we can reduce the ObjcObject.h file to this:
#import <Foundation/Foundation.h>

@interface ObjcObject : NSObject
- (void)exampleMethodWithString:(NSString*)str;
// other wrapped methods and properties
@end
The missing parts all move to the class extension in the implementation file (ObjcObject.mm):
#import "ObjcObject.h"
#import "CppObject.h"
@interface ObjcObject () {
  CppObject wrapped;
}
@end

@implementation ObjcObject
- (void)exampleMethodWithString:(NSString*)str
{
  // NOTE: if str is nil this will produce an empty C++ string
  // instead of dereferencing the NULL pointer from UTF8String. 
  std::string cpp_str([str UTF8String], [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
  wrapped.ExampleMethod(cpp_str);
}
Alternatively, if we don't need the interface extension to declare any extra properties and methods, the ivar block can also live at the start of the @implementation:
#import "ObjcObject.h"
#import "CppObject.h"

@implementation ObjcObject {
  CppObject wrapped;
}

- (void)exampleMethodWithString:(NSString*)str
{
  // NOTE: if str is nil this will produce an empty C++ string
  // instead of dereferencing the NULL pointer from UTF8String. 
  std::string cpp_str([str UTF8String], [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
  wrapped.ExampleMethod(cpp_str);
}
Either way, we now #import "ObjcObject.h" to our heart's content and use ObjcObject like any other Objective-C class. The CppObject instance for the wrapped ivar will be constructed using the default constructor when you alloc (not init) an ObjcObject, the destructor will be called on dealloc. This often isn't what you want, particularly if there isn't a (public) default constructor at all, in which case the code will fail to compile.

Managing the wrapped C++ object's lifecycle

The solution is to manually trigger construction via the new keyword, e.g.
@interface ObjcObject () {
  CppObject* wrapped; // Pointer! Will be initialised to NULL by alloc.
}
@end
@implementation ObjcObject
- (id)initWithSize:(int)size
{
  self = [super init];
  if (self)
  {
    wrapped = new CppObject(size);
    if (!wrapped) self = nil;
  }
  return self;
}
//...
If using C++ exceptions, you may want to wrap the construction in a try {...} catch {...} block and handle any construction errors. With explicit construction, we also need to explicitly destroy the wrapped object:
- (void)dealloc
{
  delete wrapped;
  [super dealloc]; // omit if using ARC
}
Note that the extra level of indirection involves an extra memory allocation. Objective-C heavily allocates and frees memory all over the place, so this one extra allocation shouldn't be a big deal. If it is, you can use placement new instead, and reserve memory within the Objective-C object via an extra char wrapped_mem[sizeof(CppObject)]; ivar, creating the instance using wrapped = new(wrapped_mem) CppObject(); and destroying it via an explicit destructor call: if (wrapped) wrapped->~CppObject();. As with any use of placement new, though, you'd better have a good reason for it. Placement new returns a pointer to the constructed object. I would personally keep that (typed) pointer in an ivar just as with regular new. The address will normally coincide with the start of the char array, so you could get away with casting that instead.

Wrapping up

Now you'll probably want to wrap a bunch of member functions with Objective-C methods, and public fields with properties whose getters and setters forward to the C++ object. Make sure that your wrapper methods only return and take parameters with C or Objective-C types. You may need to do some conversions or wrap some more C++ types. Don't forget Objective-C's special nil semantics don't exist in C++: NULL pointers must not be dereferenced.

The reverse: using Objective-C classes from C++ code

I've had some email regarding the opposite: calling into Objective-C from C++. Again the problem lies with header files. You don't want to pollute the C++ header with Objective-C types, or it can't be #included from pure C++. Let's say we want to wrap the Objective-C class ABCWidget, declared in ABCWidget.h:
#import <Foundation/Foundation.h>
@interface ABCWidget
- (void)init;
- (void)reticulate;
// etc.
@end
Once again, this kind of class definition will work in Objective-C++, but this time not in pure C++:
#import "ABCWidget.h"
namespace abc
{
  class Widget
  {
    ABCWidget* wrapped;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}
A pure C++ compiler will trip over the code in Foundation.h and eventually the @interface block for ABCWidget.

Some things never change: PIMPL

There's no such thing as a class extension in C++, so that trick won't work. PIMPL, on the other hand, works just fine and is actually quite commonly used in plain C++ anyway. In our case, we reduce the C++ class to its bare minimum:
namespace abc
{
  struct WidgetImpl;
  class Widget
  {
    WidgetImpl* impl;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}
And then, in Widget.mm:
#include "Widget.hpp"
#import "ABCWidget.h"
namespace abc
{
  struct WidgetImpl
  {
    ABCWidget* wrapped;
  };
  Widget::Widget() :
    impl(new WidgetImpl)
  {
    impl->wrapped = [[ABCWidget alloc] init];
  }
  Widget::~Widget()
  {
    if (impl)
      [impl->wrapped release];
    delete impl;
  }
  void Widget::Reticulate()
  {
    [impl->wrapped reticulate];
  }
}
This is mostly self-explanatory; the reason it works is that a forward declaration of a struct or class suffices for declaring variables or members as pointers to such struct or class objects. We only dereference the impl pointer inside Widget.mm after we fully define the WidgetImpl struct type.
Notice that I release the wrapped object in the destructor. Even if you use ARC in your project, I recommend you disable it for C++-heavy Objective-C++ files like this one. You can make your C++ code behave itself even with ARC, but it'll often be more work than just putting in the release and retain calls. You can disable ARC for individual files in XCode under the 'Build Phases' tab in the build target's properties. Fold out the 'Compile Sources' section and add -fno-objc-arc to the compiler flags for the file(s) in question.

A shortcut for wrapping Objective-C objects in C++

You may have noticed that the PIMPL solution uses two levels of indirection. If the wrapper is as thin as the one in this example, that's probably overkill. Although Objective-C types can generally not be used in plain C++, there are a few types that are actually defined in C. The id type is one of them, and it's declared in the <objc/objc-runtime.h> header. You lose what little type safety Objective-C gives you, but it does mean you can place your object pointer directly into the C++ class definition:
#include <objc/objc-runtime.h>
namespace abc
{
  class Widget
  {
    id /* ABCWidget* */ wrapped;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}
Sending messages to id isn't really advisable, as you lose a lot of the compiler's checking mechanism, particularly in the presence of ambiguities between differently-typed methods with the same selector (name) in different classes. So:
#include "Widget.hpp"
#import "ABCWidget.h"
namespace abc
{
  Widget::Widget() :
    wrapped([[ABCWidget alloc] init])
  {
  }
  Widget::~Widget()
  {
    [(ABCWidget*)impl release];
  }
  void Widget::Reticulate()
  {
    [(ABCWidget*)impl reticulate];
  }
}
Casting like this all the time is tedious and can easily hide bugs in your code, so let's try to do a better job in the header:
#ifdef __OBJC__
@class ABCWidget;
#else
typedef struct objc_object ABCWidget;
#endif

namespace abc
{
  class Widget
  {
    ABCWidget* wrapped;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}
So, if this header is #imported in a .mm file, the compiler is fully aware of the specific class type. If #included in pure C++ mode, ABCWidget* is identical to the id type: id is defined as typedef struct objc_object* id;. The #ifdef block can of course be further tidied up into a reusable macro:
#ifdef __OBJC__
#define OBJC_CLASS(name) @class name
#else
#define OBJC_CLASS(name) typedef struct objc_object name
#endif
We can now forward-declare Objective-C classes in headers usable by all 4 languages:
OBJC_CLASS(ABCWidget);

Acknowledgements

Many thanks to Christopher Atlan, Uli Kusterer and Jedd Haberstro for their suggestions and corrections after reading drafts of this article.
Thanks to Rick Mann for making a suggestion that prompted me to come up with the final version for wrapping Objective-C classes with C++.
Content Copyright 2010-2012 Phillip Jordan; Design Copyright 2010 Laura Dennis; all rights reserved
 

http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects


Strategies for Using C++ in Objective-C Projects (and vice versa)

Update (May 2012): while nothing in this article is incorrect, there have been some changes to Objective-C since clang became Apple's primary compiler. This means there is now an easier way to combine C++ and Objective-C than the techniques proposed here, as long as you're using clang and don't need to maintain GCC compatibility. I have written about the new technique and the feature enabling it in a new article. For a more extensive explanation and some alternative solutions, all of which still work with GCC, read on.
If you're in a hurry and want to get straight to the solution of embedding C++ objects in Objective-C classes without tainting the header files so they can still be included from plain Objective-C, you can skip straight to the conclusion showing the solution to use in ~95% of cases. The rest of the article contains deeper analysis of the issue at hand and alternative approaches to solving it.

Why mix Objective-C with C++?

When using Objective-C for whatever reason, typically for iOS or Mac development, I've often encountered situations where I wanted to incorporate C++ in the project in some way. Sometimes the best library for the job happens to be written in C++. Sometimes the solution to a problem is more succinctly implemented using C++. The most obvious use are C++ templates, which can save you from repeating boilerplate code. Maybe less obviously, I find that Objective-C is sometimes too object oriented. This is obviously heresy among the "everything is an object" folks, but for non-trivial data structures, I often find classical object orientation unwieldy, and C's structs just a bit too weak. C++'s model is a continuum.
Objective-C also is quite assertive about memory management, which can get in the way, at least in the absence of garbage collection. The STL (and its newer shared_ptr extension) often lets you forget about that issue altogether, or to concentrate it in constructors and destructors, rather than littering your code with retain and release. It is of course a matter of taste and applies differently to different situations; automating memory management tends to be most helpful in code with complex data structures, or code which is heavily algorithmic.
Another good reason for mixing Objective-C with C++ is the opposite situation: the need to use Objective-C libraries, such as those for the Apple platforms, from a C++ project. One common scenario for this is porting a game or engine to those platforms, and most of the following techniques can be applied in those cases too.
Finally, you might want to use C++ for performance reasons. The flexibility of Objective-C messaging adds some overhead compared to most implementations of C++ virtual functions, even with the method caching techniques used in modern runtimes. Objective-C objects don't have an equivalent of C++'s non-virtual functions, which are faster still. This can be relevant for optimising performance hotspots.

The lowest common denominator: C

One option for using the two languages in the same project is to separate them completely. You only allow them to communicate via a pure C interface, thus avoiding mixing the languages altogether. The code using the C++ library goes in a .cpp file, the code calling it is pure Objective-C (.m), the interface is declared in a C header, and the C++ side implements its interface with extern "C" functions.
This will work quite well in simple cases, but more likely than not you'll be writing quite a bit of wrapper code. Anyone with experience writing dynamically loadable C++ libraries via a public C interface knows this all too well. [1] Virtually all Objective-C seems to be compiled with GCC or clang nowadays. Both compilers support Objective-C++, usually a better means for mixing the languages.

Objective-C++ and the trouble with header files

At first glance, using the Objective-C++ dialect looks like a straightforward approach. It is the result of mashing C++ and Objective-C together in the same compiler, and robust implementations exist in GCC and now clang. Considering just how different the details of Objective-C and C++ are, the GCC hackers have done a great job of it. But as you start renaming your .m files to .mm to introduce chunks of C++, you quickly realise it's not quite so simple.
Header files and the C preprocessor in general have caused headaches for C, C++ and Objective-C programmers for decades. It gets worse when you try to mix the languages. Say you wanted to use the STL's map in an Objective-C class in your project. Apple's Foundation libraries to my knowledge don't contain a sorted, tree-based map; one of our StyleKit Components needs exactly that, for example. So we simply create an instance variable for the map in our class and away we go:
#include <map>
@interface MyClass : NSObject {
  @private
  std::map<int, id> lookupTable;
}
// ...
@end
However, std::map<int, id> [2] only makes sense to a C++-aware compiler, and only after an #include <map> [3], so this header now can only be #imported from Objective-C++ files. Any code using this class now needs to be converted to Objective-C++ itself, and importing from other headers leads to a cascade effect that quickly encompasses the whole project.
In some cases, this may be acceptable. However, switching a whole project or large parts of it across just to introduce a library which is used in one location is not only excessive; if you're the only one who knows C++ on a project with multiple Objective-C programmers, you might find this to be an unpopular idea. It might also cause issues in the same way that compiling pure C code with a C++ compiler rarely is completely hassle-free. Moreover, it means that code isn't automatically reusable in other Objective-C projects.
In most cases, using Objective-C++ only where necessary is the way to go, keeping the majority of the code in pure Objective-C or C++, but the best way of doing so was not immediately apparent to me. I also found remarkably little mention of the issue on the web.

Shooting roughly in the direction of your foot: void*

Given these problems, the objective is to remove all trace of C++, mainly member types, from header files. The typical C way of hiding types is to use a pointer to void. This will certainly work here, too.
@interface MyClass : NSObject {
  @private
  // is actually a  std::map<int, id>*
  void* lookupTable;
}
// ...
@end
In the code using the table, we always have to cast to the correct type using static_cast<std::map<int, id>*>(lookupTable) or ((std::map<int, id>*)lookupTable), which is annoying at best. If the actual type of the member ends up changing, all the casts must be changed manually - an error prone process. With a growing number of members, keeping track of the correct types becomes infeasible. You really are getting the worst of both worlds from static and dynamic typing. If you use this approach when dealing with objects in a class hierarchy, you're dicing with death outright due to the possibility that an A* and a B* to the same object don't have identical void* representations.[4]
Suffice to say, we can do better.

Conditional compilation

Losing type information sucks, but since we can only use the C++ typed fields from Objective-C++ code, and the pure Objective-C compiler only needs to be aware of their presence for the purposes of memory layout, we can provide 2 different versions of the code. The preprocessor symbol __cplusplus is defined in Objective-C++ mode, so how about this:
#ifdef __cplusplus
#include <map>
#endif
@interface MyClass : NSObject {
  @private
#ifdef __cplusplus
  std::map<int, id>* lookupTable;
#else
  void* lookupTable;
#endif
}
// ...
@end
It's not pretty, but it's much easier to work with. The C++ standard probably doesn't guarantee that a class-pointer and a void-pointer have the same memory properties, but Objective-C++ is a non-standard GNU/Apple thing anyway. I've only found pointers to virtual member functions to be a problem when converting to void* in practice, and the compiler will complain loudly if you attempt this. If you're worried, use static_cast<> instead of C-style casts.
Still, C happily casts void* to other pointer types implicitly, so it may be preferable to replace the C part of the #ifdef with a pointer to an opaque struct with a unique and recogniseable name, e.g. struct MyPrefix_std_map_int_id. You can even define a macro which expands to the correct type depending on the compiler's language. You can't auto-mangle templated types in a C macro though, and you'll struggle with multiple levels of namespace nesting.
#ifdef __cplusplus
#define OPAQUE_CPP_TYPE(cpptype, ctype) cpptype
#else
#define OPAQUE_CPP_TYPE(cpptype, ctype) struct ctype
#endif

// ...

#ifdef __cplusplus
#include <map>
#endif

@interface MyClass : NSObject {
  @private
  OPAQUE_CPP_TYPE(std::map<int, id>, cpp_std_map_int_id)* lookupTable;
}
// ...
@end
You can't avoid conditionally including C++ headers with this method, and you may confuse/upset those who don't know/like C++, and it's all rather ugly. Luckily, there are other options.

Abstract classes, interfaces and protocols

As a C++ programmer, you're probably familiar with pure virtual functions and, as a consequence, abstract classes. Other langages such as Java and C# have an explicit "interface" concept which deliberately leaves out any implementation details. Hiding the implementation is exactly what we're trying to do, so can we use a similar pattern here?
Recent versions of Objective-C do support protocols, which are similar to Java/C# interfaces in spirit, if not in syntax. [5] We could specify our class's public methods in a protocol in the header and specify and implement the concrete class conforming to said protocol in private code. This works well for instance methods, but there's clearly no way to directly create class instances via the protocol. You therefore need to delegate the task of allocating and initialising new objects to a factory object or a free C function. Worse, protocols are orthogonal to the class hierarchy, so reference declarations will look different than those for other types:
id<MyProtocol> ref;
Instead of the expected
MyClass* ref;
Workable, but probably not ideal.

True abstract classes in Objective-C

So what about abstract classes? There's no direct, idiomatic support for them in the language, but even the very prominent NSString is abstract, and you wouldn't know it just by using it. It turns out that documentation on the subject is scarce. One option is to leave out the method implementations in the abstract base altogether and live with the compiler warning about the incomplete class. At runtime, attempts to call unspecified methods will raise exceptions. More helpfully, but also much more laboriously, you can create dummy implementations which do nothing but raise an exception explaining the situation.
In most languages you need to know the concrete class when creating instances, or delegate this task to a factory. Interestingly, you can alloc and init NSString directly and receive subclass instances! As far as I know, the init methods return a different object than the self they are given, or they're internally doing funky things with the object's type. Alternatively, NSString's alloc class method could be overridden to call its NSCFString counterpart, NSString thus acting as a factory for its own subclasses. If you do this yourself, you'll also need to define all the init* methods the concrete class uses on the abstract class, too, or they won't be visible to users of your class.
So far, this is definitely the cleanest solution for the header file and for users of the class, but it's also by far the most laborious, requiring an extra class, dummy/abstract methods and complicated init wrangling.
However, C++ programmers have found an elegant solution to similar problems. Exploding compile times due to seemingly infinite header dependencies are a serious issue in large C++ projects, and hiding class internals from library users is also often desired. As it happens, the solution to these can be applied to the Obective-C/C++ conundrum.

Pimpl

Short for "pointer to implementation" or "private implementation," this idiom is less unpleasant than its name might suggest at first. It's well-documented in the C++ literature. It's also quite simple. In the public header, add a forward declaration of an implementation struct, typically using the public class's name suffixed with "Impl" or some such convention. This struct will hold all the members we want to hide from the public class's header. Add a pointer to the struct as a class instance variable, and define the struct's members in the .cpp file (or in our case, the .mm), not the header. On construction (here: -init*), construct an instance of the struct using the new operator and set the instance variable to it, and ensure delete is called on destruction (here: in -dealloc).
In MyClass.h:
// ...
struct MyClassImpl;
@interface MyClass : NSObject {
  @private
  struct MyClassImpl* impl;
}
// public method declarations...
- (id)lookup:(int)num;
// ...
@end
In MyClass.mm:
#import "MyClass.h"
#include <map>
struct MyClassImpl {
  std::map<int, id> lookupTable;
};

@implementation MyClass
- (id)init
{
  self = [super init];
  if (self)
  {
    impl = new MyClassImpl;
  }
  return self;
}
- (void)dealloc
{
  delete impl;
}
- (id)lookup:(int)num
{
  std::map<int, id>::const_iterator found =
    impl->lookupTable.find(num);
  if (found == impl->lookupTable.end()) return nil;
  return found->second;
}
// ...
@end
This works because forward declarations of structs are valid C code, even if the struct later turns out to have implicit or explicit C++ constructors, or even base classes. The public class's methods access the contents of the struct via the pointer, possibly via member functions of the struct. Construction and destruction of members is handled by the new and delete operators as long as they are called correctly.
It's more or less up to you whether the functionality lives in member functions of the public class or the implementation class, or both. You might gain some efficiency by avoiding Objective-C messages in some cases, but it can get messy if the C++ methods need to call the Objective-C class's.
It should be noted that instead of single-member implementation structs, the implementation class can actually derive from the type of the member instead, thus avoiding the indirection on method calls. For example, in MyClass.h:
@interface MyClass : NSObject {
  struct MyClassMap* lookupTable;
}
- (id)lookup:(int)i;
@end
and MyClass.mm:
#import "MyClass.h"
#include <map>
struct MyClassMap : std::map<int, id> { };
@implementation MyClass
- (id)init {
  self = [super init];
  if (self)
    lookupTable = new MyClassMap;
  return self;
}
- (id)lookup:(int)i {
  MyClassMap::const_iterator found = lookupTable->find(i);
  return (found == lookupTable->end()) ? nil : found->second;
}
- (void)dealloc {
  delete lookupTable; lookupTable = NULL;
  [super dealloc];
}
@end
This becomes less practical with larger numbers of members due to the many new/delete pairs required. Locality of reference may also be worse.

Limitations

In pure C++, you may of course declare the implementation as a class, though this clearly won't work in our case where the forward declaration must be valid Objective-C. In the C++ literature, you may also find recommendations to use shared_ptr<> or auto_ptr<> to handle automatic deletion of the implementation object, or even to use a mix-in (templated base class) to provide the functionality. Neither will work with Objective-C headers. Even in runtimes that support correct construction/destruction of C++ members, the implementation member must be a pointer, as the struct type is incomplete in the header and reserving the correct amount of memory in the class will fail.
Because the implementation's definition is private, accessing members from a derived class, i.e. protected members, isn't directly possible. You can however move the definition of the implementation struct to a semi-private header, which only needs to be included by subclasses requiring direct access to it. Those subclasses must therefore be written in Objective-C++ themselves. Extending the implementation by subclassing it will be tricky as the pointer will still have the superclass type; you might prefer to create a second, separate struct instead.

Final thoughts

Nevertheless, I've found the Pimpl idiom to be the best choice for embedding C++ in Objective-C in almost all cases. Even when the struct only contains one member, the lack of casts easily makes up for the indirection. No efficiency is lost as long as the struct member isn't a pointer itself. For the reverse case of embedding Objective-C in a C++ class, use a similar approach. Define the public interface of the C++ class and a forward declaration for the implementation class in the header and place its definition with Objective-C member types in the corresponding .mm file.
A real-world example of embedding C++ can be found in my Objective-C wrapper for the Open-VCDiff decoder.

An Update (May 2012)

Since I first wrote this article in November 2010, Apple has switched to the clang compiler and made some changes to the Objective-C language. One of the new features, the class extension, allows you to declare instance variables outside the header file, opening up new possibilities for language mixing. I have written a new article explaining the class extension technique - follow the link to read more on the subject.

Acknowledgements

Thanks to Björn Knafla for the valuable suggestions after reading drafts of this article and for the Twitter discussion which ultimately lead to writing this article in the first place.
Thanks also go to Markus Prinz for reading drafts of this article and making helpful suggestions.

References and Footnotes

[1] e.g. Chapter 7, Matthew Wilson, Imperfect C++; 2005 Addison-Wesley
[2] Note that in earlier versions of Objective-C++, lookupTable would have had to be a pointer and the map would need to be constructed explicitly with new, as the STL map has a non-trivial constructor. Clean C++ member construction and destruction in the Objective-C object lifecycle is a recent addition to Apple's implementation. I haven't tested it on other implementations of the runtime.
[3] You can forward declare std::map in theory but lose the ability to use the defaults for the third and fourth template parameters by doing so.
[4] I have so far only encountered this in connection with multiple inheritance. Still, class hierarchies can change, and memory layouts tend to be implementation-defined.
[5] In line with the rest of Objective-C, protocols also "looser" and more dynamic than interfaces in C#/Java, but that doesn't change anything in this case.

Tuesday, May 31, 2016

EXIF info

General knowledge
http://www.media.mit.edu/pia/Research/deepview/exif.html


Two utilities.

exiftool: Perl
http://www.sno.phy.queensu.ca/~phil/exiftool/

exiv2: C++

http://www.exiv2.org/

exiftool is better than exiv2 in terms of extracting more information. 

For a sample GPS.JPG,


exiv2 output
~/exiv2-0.25/bin$ ./exiv2 GPS.jpg
File name       : GPS.jpg
File size       : 2133 Bytes
MIME type       : image/jpeg
Image size      : 120 x 80
Camera make     : FUJIFILM
Camera model    : FinePixS1Pro
Image timestamp : 2002:07:13 15:58:28
Image number    :
Exposure time   : 1/724 s
Aperture        : F0.64
Exposure bias   : -1090519041/1677721600 EV
Flash           : No flash
Flash bias      :
Focal length    : 0.0 mm
Subject distance:
ISO speed       :
Exposure mode   : Shutter priority
Metering mode   : Multi-segment
Macro mode      :
Image quality   :
Exif Resolution : 2400 x 1600
White balance   :
Thumbnail       : image/jpeg, 28 Bytes
Copyright       : ian Britton - FreeFoto.com
Exif comment    :

exiftool output
~/exiv2-0.25/bin$ ./exiv2 GPS.jpg
File name       : GPS.jpg
File size       : 2133 Bytes
MIME type       : image/jpeg
Image size      : 120 x 80
Camera make     : FUJIFILM
Camera model    : FinePixS1Pro
Image timestamp : 2002:07:13 15:58:28
Image number    :
Exposure time   : 1/724 s
Aperture        : F0.64
Exposure bias   : -1090519041/1677721600 EV
Flash           : No flash
Flash bias      :
Focal length    : 0.0 mm
Subject distance:
ISO speed       :
Exposure mode   : Shutter priority
Metering mode   : Multi-segment
Macro mode      :
Image quality   :
Exif Resolution : 2400 x 1600
White balance   :
Thumbnail       : image/jpeg, 28 Bytes
Copyright       : ian Britton - FreeFoto.com
Exif comment    :


EXIF tags:
http://www.exiv2.org/tags.html


Write comments
Both can read/write comments.
./exiv2 -M"set Exif.Photo.UserComment charset=Ascii testtest" test.jpg

============
http://stackoverflow.com/questions/8937963/save-custom-metadata-in-an-image-taken-from-avfoundation-in-ios