[issue1522400] irda socket support

Charles-François Natali report at bugs.python.org
Tue Apr 24 22:00:23 CEST 2012


Charles-François Natali <neologix at free.fr> added the comment:

Here's a cleanup up patch against default.
However, I don't have any IrDA capable device neither, so I won't be
able to help much. I'll ask on python-dev if someone can help.
As for the manual decoding, AFAICT you'll have the same issue with CAN sockets.
The real question is really "how high-level should the socket module be?".
Because if we added let's say and IRDASocket type, then we could
easily add helper functions to and methods to e.g. list available IrDA
devices, decode fields, etc.
Same thing holds for CAN sockets, and one could imagine we could also
maybe add a MulticastSocket that would make joining/leaving a
multicast group easy (because it is a pain), etc.

----------
Added file: http://bugs.python.org/file25349/irda-default.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1522400>
_______________________________________
-------------- next part --------------
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1262,6 +1262,16 @@
         }
 #endif
 
+#ifdef AF_IRDA
+    case AF_IRDA:
+    {
+        struct sockaddr_irda *a = (struct sockaddr_irda *)addr;
+
+        return Py_BuildValue("iO&", a->sir_addr,
+                                    PyUnicode_DecodeFSDefault, a->sir_name);
+    }
+#endif
+
     /* More cases here... */
 
     default:
@@ -1759,6 +1769,47 @@
         }
 #endif
 
+#ifdef HAVE_IRDA_H
+    case AF_IRDA:
+    {
+        struct sockaddr_irda *addr;
+        int daddr;
+        PyObject *service;
+        addr = (struct sockaddr_irda *)addr_ret;
+        Py_ssize_t len;
+
+        if (!PyArg_ParseTuple(args, "iO&", &daddr,
+                                           PyUnicode_FSConverter, &service))
+            return 0;
+
+        len = PyBytes_GET_SIZE(service);
+
+        if (len <= 0 || len >= sizeof(addr->sir_name)) {
+            PyErr_SetString(PyExc_OSError, "AF_IRDA service name too long");
+            Py_DECREF(service);
+            return 0;
+        }
+
+#ifdef HAVE_LINUX_IRDA_H
+        addr->sir_family = AF_IRDA;
+        addr->sir_lsap_sel = LSAP_ANY;
+        addr->sir_addr = daddr;
+        strcpy(addr->sir_name, PyBytes_AS_STRING(service));
+#else /* Windows */
+        addr->irdaAddressFamily = AF_IRDA;
+        addr->irdaDeviceID[0] = (daddr >> 24) & Oxff;
+        addr->irdaDeviceID[1] = (daddr >> 16) & Oxff;
+        addr->irdaDeviceID[2] = (daddr >> 8) & Oxff;
+        addr->irdaDeviceID[3] = daddr & Oxff;
+        strcpy(addr->irdaServiceName, PyBytes_AS_STRING(service));
+#endif
+
+        Py_DECREF(service);
+        *len_ret = sizeof(*addr);
+        return 1;
+    }
+#endif
+
     /* More cases here... */
 
     default:
@@ -1880,6 +1931,14 @@
         }
 #endif
 
+#ifdef AF_IRDA
+    case AF_IRDA:
+    {
+        *len_ret = sizeof (struct sockaddr_irda);
+        return 1;
+    }
+#endif
+ 
     /* More cases here... */
 
     default:
@@ -5823,6 +5882,14 @@
     PyModule_AddIntConstant(m, "AF_SYSTEM", AF_SYSTEM);
 #endif
 
+/* Infrared Data Association */
+#ifdef AF_IRDA
+    PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
+#endif
+#ifdef PF_IRDA
+    PyModule_AddIntConstant(m, "PF_IRDA", PF_IRDA);
+#endif
+
 #ifdef AF_PACKET
     PyModule_AddIntMacro(m, AF_PACKET);
 #endif
@@ -5895,6 +5962,13 @@
     PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
 #endif
 
+#ifdef HAVE_IRDA_H
+    PyModule_AddIntConstant(m, "SOL_IRLMP", SOL_IRLMP);
+    PyModule_AddIntConstant(m, "IRLMP_ENUMDEVICES", IRLMP_ENUMDEVICES);
+    PyModule_AddIntConstant(m, "IRLMP_IAS_SET", IRLMP_IAS_SET);
+    PyModule_AddIntConstant(m, "IRLMP_IAS_QUERY", IRLMP_IAS_QUERY);
+#endif
+
     /* Socket types */
     PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
     PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h
--- a/Modules/socketmodule.h
+++ b/Modules/socketmodule.h
@@ -87,6 +87,17 @@
 #include <sys/kern_control.h>
 #endif
 
+#if (defined(MS_WINDOWS) || defined(HAVE_LINUX_IRDA_H))
+#define HAVE_IRDA_H
+#ifdef HAVE_LINUX_IRDA_H
+#include <linux/types.h>
+#include <linux/irda.h>
+#else
+#include <af_irda.h>
+#define sockaddr_irda _SOCKADDR_IRDA
+#endif
+#endif
+
 #ifndef Py__SOCKET_H
 #define Py__SOCKET_H
 #ifdef __cplusplus
@@ -148,6 +159,9 @@
 #ifdef HAVE_SYS_KERN_CONTROL_H
     struct sockaddr_ctl ctl;
 #endif
+#ifdef HAVE_IRDA_H
+    struct sockaddr_irda ir;
+#endif
 } sock_addr_t;
 
 /* The object holding a socket.  It holds some extra information,
diff --git a/configure b/configure
--- a/configure
+++ b/configure
@@ -6605,6 +6605,25 @@
 done
 
 
+# On Linux, irda.h requires sys/socket.h
+for ac_header in linux/irda.h
+do :
+  ac_fn_c_check_header_compile "$LINENO" "linux/irda.h" "ac_cv_header_linux_irda_h" "
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+"
+if test "x$ac_cv_header_linux_irda_h" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_LINUX_IRDA_H 1
+_ACEOF
+
+fi
+
+done
+
+
 # checks for typedefs
 was_it_defined=no
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_t in time.h" >&5
diff --git a/configure.ac b/configure.ac
--- a/configure.ac
+++ b/configure.ac
@@ -1404,6 +1404,13 @@
 #endif
 ])
 
+# On Linux, irda.h requires sys/socket.h  
+AC_CHECK_HEADERS(linux/irda.h,,,[   
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h> 
+#endif
+])
+
 # checks for typedefs
 was_it_defined=no
 AC_MSG_CHECKING(for clock_t in time.h)
diff --git a/pyconfig.h.in b/pyconfig.h.in
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -501,6 +501,9 @@
 /* Define to 1 if you have the <linux/can/raw.h> header file. */
 #undef HAVE_LINUX_CAN_RAW_H
 
+/* Define to 1 if you have the <linux/irda.h> header file. */
+#undef HAVE_LINUX_IRDA_H
+
 /* Define to 1 if you have the <linux/netlink.h> header file. */
 #undef HAVE_LINUX_NETLINK_H
 


More information about the Python-bugs-list mailing list