def request(self,command,timeout=0.1): """Send a 'get'-type command to the controller and return the reply.""" written = ctypes.c_ulong() read = ctypes.c_ulong() wr_buf = ctypes.create_string_buffer(command,len(command)) time.sleep(0.1) if dll.FT_Write(self.usb, wr_buf, ctypes.c_ulong(len(wr_buf)), ctypes.byref(written)): raise RuntimeError('USB write failed.') if self.wait(6,timeout) < 6: # wait for 6 bytes reply message raise RuntimeError('USB request timed out after %fs.'%timeout) head_buf = ctypes.create_string_buffer(6) if dll.FT_Read(self.usb, head_buf, ctypes.c_ulong(6), ctypes.byref(read)): raise RuntimeError('USB read failed.') id,length,dest,source=struct.unpack('<2H2B',head_buf.raw) # convert message assuming that it is the header for a data if dest>>7: # a data packet will follow if self.wait(length,timeout) < length: # wait for expected number of bytes raise RuntimeError('USB request timed out after %fs.'%timeout) rd_buf = ctypes.create_string_buffer(length) if dll.FT_Read(self.usb, rd_buf, ctypes.c_ulong(length), ctypes.byref(read)): raise RuntimeError('USB read failed.') self.ack() return head_buf.raw+rd_buf.raw else: # simple 6 byte message self.ack() return head_buf.raw
def open_ex(serial=''): '''open's FTDI-device by EEPROM-serial (prefered method). Serial fetched by the ListDevices fn''' ftHandle = c.c_ulong() dw_flags = c.c_ulong(FT_OPEN_BY_SERIAL_NUMBER) _PY_OpenEx(serial, dw_flags, c.byref(ftHandle)) return FTD2XX(ftHandle)
def _login_iscsi_target(self, target_name, portal=None, login_opts=None, is_persistent=True, initiator_name=None): session_id = iscsi_struct.ISCSI_UNIQUE_SESSION_ID() connection_id = iscsi_struct.ISCSI_UNIQUE_CONNECTION_ID() portal_ref = ctypes.byref(portal) if portal else None login_opts_ref = ctypes.byref(login_opts) if login_opts else None initiator_name_ref = (ctypes.c_wchar_p(initiator_name) if initiator_name else None) # If the portal is not provided, the initiator will try to reach any # portal exporting the requested target. self._run_and_check_output( iscsidsc.LoginIScsiTargetW, ctypes.c_wchar_p(target_name), False, # IsInformationalSession initiator_name_ref, ctypes.c_ulong(iscsi_struct.ISCSI_ANY_INITIATOR_PORT), portal_ref, iscsi_struct.ISCSI_SECURITY_FLAGS( iscsi_struct.ISCSI_DEFAULT_SECURITY_FLAGS), None, # Security flags / mappings (using default / auto) login_opts_ref, ctypes.c_ulong(0), None, # Preshared key size / key is_persistent, ctypes.byref(session_id), ctypes.byref(connection_id), ignored_error_codes=[iscsierr.ISDSC_TARGET_ALREADY_LOGGED_IN]) return session_id, connection_id
def get_firmware_version(self): version = c_ulong() sha = c_ulong() timestamp = c_ulong() string = create_string_buffer(64) check(libaps2.get_firmware_version(self.ip_address.encode('utf-8'), byref(version), byref(sha), byref(timestamp), string)) return version.value, sha.value, timestamp.value, string.value.decode('ascii')
def inner(*args, **kwargs): call_args = inspect.getcallargs(f, *args, **kwargs) call_args['element_count'] = ctypes.c_ulong(0) call_args['buff'] = (ctypes.c_ubyte * 0)() call_args['buff_size'] = ctypes.c_ulong(0) while True: try: ret_val = f(**call_args) if parse_output: return _get_items_from_buff( call_args['buff'], struct_type, call_args['element_count'].value) else: return ret_val except exceptions.Win32Exception as ex: if (ex.error_code & 0xFFFF) == ERROR_INSUFFICIENT_BUFFER: if func_requests_buff_sz: buff_size = call_args['buff_size'].value else: buff_size = (ctypes.sizeof(struct_type) * call_args['element_count'].value) call_args['buff'] = (ctypes.c_ubyte * buff_size)() else: raise
def _alloc_C_libcall(self, size, ctype): if not self.available(): raise RuntimeError("Couldn't find `libnuma`'s `numa_alloc_*` to " "allocate memory") if size == 0: # work around the fact that the allocator may return NULL when # the size is 0, and numpy does not like that c_bytesize = ctypes.c_ulong(1) else: c_bytesize = ctypes.c_ulong(size * ctypes.sizeof(ctype)) if self.put_onnode: c_pointer = self.lib.numa_alloc_onnode(c_bytesize, self._node) elif self.put_local: c_pointer = self.lib.numa_alloc_local(c_bytesize) else: c_pointer = self.lib.numa_alloc(c_bytesize) # note! even though restype was set above, ctypes returns a # python integer. # See https://stackoverflow.com/questions/17840144/ # edit: it apparently can return None, also! if c_pointer == 0 or c_pointer is None: return None, None else: # Convert it back to a void * - this is # _very_ important when later # passing it to numa_free c_pointer = ctypes.c_void_p(c_pointer) return c_pointer, (c_pointer, c_bytesize)
def interceptWinProc(self, interceptCollection, params): """ Called for each Windows message to the intercepted window. This is the ANSI-style method, wide-char is not supported. params -- WinProcParams object containing parameters the function can modify and a returnValue which can be set to prevent from calling interceptWinProc functions """ if params.uMsg == WM_CHANGECBCHAIN: if self.nextWnd == params.wParam: # repair the chain self.nextWnd = params.lParam if self.nextWnd: # Neither None nor 0 # pass the message to the next window in chain SendMessage(c_int(self.nextWnd), c_int(params.uMsg), c_uint(params.wParam), c_ulong(params.lParam)) elif params.uMsg == WM_DRAWCLIPBOARD: if self.ignoreNextCCMessage: self.ignoreNextCCMessage = False else: self.handleClipboardChange() if self.nextWnd: # Neither None nor 0 # pass the message to the next window in chain SendMessage(c_int(self.nextWnd), c_int(params.uMsg), c_uint(params.wParam), c_ulong(params.lParam))
def get_3d_attributes(self): """Retrieves the 3D attributes of a sample, stream, or MOD music channel with 3D functionality.""" answer = dict(mode=c_ulong(), min=c_float(), max=c_float(), iangle=c_ulong(), oangle=c_ulong(), outvol=c_float()) bass_call(BASS_ChannelGet3DAttributes, self.handle, pointer(answer['mode']), pointer(answer['min']), pointer(answer['max']), pointer(answer['iangle']), pointer(answer['oangle']), pointer(answer['outvol'])) for k in answer: answer[k] = answer[k].value() return answer
def _task_list(): psapi = ctypes.windll.psapi kernel = ctypes.windll.kernel32 hModule = ctypes.c_ulong() count = ctypes.c_ulong() modname = ctypes.c_buffer(30) PROCESS_QUERY_INFORMATION = 0x0400 PROCESS_VM_READ = 0x0010 pid_list = win32process.EnumProcesses() info_list = [] for pid in pid_list: hProcess = kernel.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid) if hProcess: psapi.EnumProcessModules(hProcess, ctypes.byref(hModule), ctypes.sizeof(hModule), ctypes.byref(count)) psapi.GetModuleBaseNameA(hProcess, hModule.value, modname, ctypes.sizeof(modname)) pname = ctypes.string_at(modname) procmeminfo = win32process.GetProcessMemoryInfo(hProcess) procmemusage = (procmeminfo["WorkingSetSize"]/1024) info_list.append((pid, pname, procmemusage)) kernel.CloseHandle(hProcess) return info_list
def EnumMissingModules(): """Enumerate all modules which match the patterns MODULE_PATTERNS. PyInstaller often fails to locate all dlls which are required at runtime. We import all the client modules here, we simply introdpect all the modules we have loaded in our current running process, and all the ones matching the patterns are copied into the client package. Yields: a source file for a linked dll. """ module_handle = ctypes.c_ulong() count = ctypes.c_ulong() process_handle = ctypes.windll.kernel32.OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, os.getpid()) ctypes.windll.psapi.EnumProcessModules( process_handle, ctypes.byref(module_handle), ctypes.sizeof(module_handle), ctypes. byref(count)) # The size of a handle is pointer size (i.e. 64 bit of amd64 and 32 bit on # i386). if sys.maxsize > 2 ** 32: handle_type = ctypes.c_ulonglong else: handle_type = ctypes.c_ulong module_list = (handle_type * (count.value / ctypes.sizeof(handle_type)))() ctypes.windll.psapi.EnumProcessModulesEx( process_handle, ctypes.byref(module_list), ctypes.sizeof(module_list), ctypes.byref(count), 2) for x in module_list: module_filename = win32process.GetModuleFileNameEx(process_handle, x).lower() if "winsxs" in module_filename or "site-packages" in module_filename: yield module_filename
def ReleaseKey(hexKeyCode): if hexKeyCode != 0x0 : extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) ) x = Input( ctypes.c_ulong(1), ii_ ) ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def fill_to_eol_with_bg_color(): if is_windows: cbuf = CONSOLE_SCREEN_BUFFER_INFO() stdout_handle = ctypes.windll.kernel32.GetStdHandle(ctypes.c_ulong(-11)) ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, ctypes.byref(cbuf)) cursor = cbuf.dwCursorPosition distance = cbuf.srWindow.Right - cursor.X # distance > 0 skips x == right to avoid default windows scroll-on-last-col-write behavior if distance > 0: cbuf = CONSOLE_SCREEN_BUFFER_INFO() stdout_handle = ctypes.windll.kernel32.GetStdHandle(ctypes.c_ulong(-11)) ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, ctypes.byref(cbuf)) cursor = cbuf.dwCursorPosition temp_cursor = COORD() written = ctypes.c_uint(0) char_attr = ctypes.c_uint16(cbuf.wAttributes) space = ctypes.c_char_p(b' ') for i in range(distance): temp_cursor.X = cursor.X + i temp_cursor.Y = cursor.Y ctypes.windll.kernel32.WriteConsoleOutputAttribute(stdout_handle, ctypes.byref(char_attr), 1, temp_cursor, ctypes.byref(written)) ctypes.windll.kernel32.WriteConsoleOutputCharacterA(stdout_handle, space, 1, temp_cursor, ctypes.byref(written)) else: sys.stdout.write('\x1b[K') # insure bg_col covers rest of line
def ReleaseKey(hexKeyCode): extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) ) x = Input( ctypes.c_ulong(1), ii_ ) SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def StartTransfers(self): size = ctypes.c_ulong() _DLL.ADI_DBG_GetNumReads(self.handle, ctypes.byref(size)) read = ctypes.c_ulong() words = (ctypes.c_ulong * size.value)() _DLL.ADI_DBG_StartTransfers(self.handle, words, size, ctypes.byref(read)) return list(words)
def copy(self): if self.__crop: x1, y1, x2, y2 = self.__crop width, height = self.__resize level = desired_progression_level(x1, x2, y1, y2, width, height) aw_j2k_set_input_j2k_resolution_level(self._j2k_object_p, level, FULL_XFORM_FLAG) data_p = ctypes.pointer(ctypes.POINTER(ctypes.c_char)()) data_length = ctypes.c_size_t() rows = ctypes.c_ulong() cols = ctypes.c_ulong() nChannels = ctypes.c_ulong() bpp = ctypes.c_ulong() aw_j2k_get_output_image_raw( self._j2k_object_p, data_p, ctypes.byref(data_length), ctypes.byref(rows), ctypes.byref(cols), ctypes.byref(nChannels), ctypes.byref(bpp), 0, ) data = data_p.contents[0 : data_length.value] image = PILImage.frombuffer("L", (cols.value, rows.value), data, "raw", "L", 0, 1) aw_j2k_free(self._j2k_object_p, data_p.contents) return image
def GetHidLibraryVersion(): """Returns the SLABHIDDevice library version number as a string.""" major = ctypes.c_ulong() minor = ctypes.c_ulong() release = ctypes.c_ulong() _DLL.ADI_GetHidLibraryVersion(ctypes.byref(major), ctypes.byref(minor), ctypes.byref(release)) return "{}.{}.{}".format(major.value, minor.value, release.value)
def vid_pid(self): vid = ctypes.c_ulong(0) pid = ctypes.c_ulong(0) rel = ctypes.c_ulong(0) if self.IsOpened(): _DLL.ADI_GetOpenedAttributes(self.handle, ctypes.byref(vid), ctypes.byref(pid), ctypes.byref(rel)) return tuple([vid.value, pid.value])
def readMem(self, address, idnum = None): """ Name: U12.readMem(address, idnum=None) Args: See section 4.36 of the User's Guide Desc: Reads 4 bytes from a specified address in the LabJack's nonvolatile memory >>> dev = U12() >>> dev.readMem(0) >>> [5, 246, 16, 59] """ if address is None: raise Exception, "Must give an Address." if idnum is None: idnum = self.id ljid = ctypes.c_ulong(idnum) ad0 = ctypes.c_ulong() ad1 = ctypes.c_ulong() ad2 = ctypes.c_ulong() ad3 = ctypes.c_ulong() ec = staticLib.ReadMem(ctypes.byref(ljid), ctypes.c_long(address), ctypes.byref(ad3), ctypes.byref(ad2), ctypes.byref(ad1), ctypes.byref(ad0)) if ec != 0: raise U12Exception(ec) addr = [0] * 4 addr[0] = int(ad3.value & 0xff) addr[1] = int(ad2.value & 0xff) addr[2] = int(ad1.value & 0xff) addr[3] = int(ad0.value & 0xff) return addr
def memory_read(pid, address, size): """ read process memory :param int pid: process id for investigating :param int address: start memory address :param int size: block size to read :rtype: None :return: None """ read_buffer = create_string_buffer(size) # c_char_p("data to read") buffer_size = c_ulong(size) bytes_read = c_ulong(0) process_handle = OpenProcess(PROCESS_ALL_ACCESS, False, pid) if not process_handle: print("Process can not be opened: %d" % get_last_error()) return state = ReadProcessMemory(process_handle, address, read_buffer, buffer_size, byref(bytes_read)) if state: # print("state: %d" % state) # print("something found: %s" % read_buffer.raw) print("bytes read: %d" % buffer_size.value) if bytes_read.value == size and len(read_buffer.raw) == size: data = struct.unpack('I' * (size / 4), read_buffer.raw) print(" ".join(['0x%08x' % x for x in data])) else: pass else: print("fail, get last error: %d, state: %d" % ( get_last_error(), (state or 0))) CloseHandle(process_handle)
def PressKey(hexKeyCode): extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra) ) x = Input( ctypes.c_ulong(1), ii_ ) ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def GetModuleFileName(pid): """Get the filename for a process using GetModuleFileNameEx""" PROCESS_QUERY_INFORMATION = 0x400 PROCESS_VM_READ = 0x0010 kernel = ctypes.windll.kernel32 psapi = ctypes.windll.psapi name = ctypes.c_buffer(255) module = ctypes.c_ulong() count = ctypes.c_ulong() process = kernel.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid) # We can expect to not be able to open every process. if not process: return None # Get a handle to the first module. psapi.EnumProcessModules(process, ctypes.byref(module), ctypes.sizeof(module), ctypes.byref(count)) # Get the module file name. psapi.GetModuleFileNameExA(process, module.value, name, ctypes.sizeof(name)) # Convert the result into a decent string. result = "".join([x for x in name if x != '\x00']) kernel.CloseHandle(process) return result
def _Inject(hProcs): buf = Kernel32.VirtualAllocEx(hProcs, 0, len(dll), VIRTUAL_MEM, PAGE_READWRITE) Kernel32.WriteProcessMemory(hProcs, buf, dll, len(dll), ctypes.byref(ctypes.c_ulong())) hLib = Kernel32.GetProcAddress(Kernel32._handle, "LoadLibraryA") r_tid = ctypes.c_ulong() Kernel32.CreateRemoteThread(hProcs, None, 0, hLib, buf, 0, ctypes.byref(r_tid)) return (r_tid and True or False)
def open_ex_by_name(name): '''open's FTDI-device by EEPROM-serial (prefered method). Serial fetched by the ListDevices fn''' ftHandle = c.c_ulong() dw_flags = c.c_ulong(FT_OPEN_BY_DESCRIPTION) _PY_OpenEx(c.c_char_p(name), dw_flags, c.byref(ftHandle)) return FTD2XX(ftHandle)
def writeModifiedMemAndReturnToViewMode(self): memBytesAtEnteringWriteMode = ( genUtils.memChunksAsHexStrToLittleEndianBytes( self.memBinReprAtEnteringWriteMode)) binMemTextWidgBytes = genUtils.memChunksAsHexStrToLittleEndianBytes( self.memBinReprTextWidg.get('1.0', tkinter.END)) for byteOffset, (origByte, currByte) in enumerate(zip( memBytesAtEnteringWriteMode, binMemTextWidgBytes)): if origByte != currByte: cBytesToWriteBuf = ctypes.create_string_buffer( bytes([currByte])) cNumOfBytesWritten = ctypes.c_ulong() writeProcessMemoryResultCode = cWriteProcessMemory( self.watchedProcessHandle, self.memToViewAddrStack[ self.memToViewAddrStackPtrAsInd] + byteOffset, cBytesToWriteBuf, ctypes.c_ulong(1), ctypes.byref(cNumOfBytesWritten)) if not writeProcessMemoryResultCode: errorCode = cGetLastError() errorStr = WINDOWS_ERROR_CODE_TO_ERROR_NAME_DICT.get( errorCode, hex(errorCode)) tkinter.messagebox.showerror( title='Error', message='Failed to write to process memory.\n' 'GetLastError: {}'.format(errorStr)) return self.setViewerMode('view')
def testAACI(self): """Test confidence interval of association analysis""" dtype = numpy.dtype('uint' + str(ctypes.sizeof(ctypes.c_long) * 8)) n_lags = 5 n_p1 = 10 n_surr = 8 n_assoc = numpy.zeros(shape=(n_lags, n_p1), dtype=dtype) #Just constructing something that could be reasonable... #First five processes: single association where the lag index #is the p1 index for i in range(5): n_assoc[i, i] = 1 #Next 5: one assoc where lag is p1-1, two where lag is p1 for i in range(5, 10): n_assoc[i-5, i] = 2 if i > 5: n_assoc[i-6, i] = 1 n_assoc = n_assoc.reshape((-1,)) surr_assoc_total = numpy.zeros(shape=(n_lags * n_surr,), dtype=dtype) #Force the seed seeds = numpy.zeros(shape=(n_lags,), dtype=dtype) spacepy.lib.aa_ci(n_assoc.ctypes.data_as(spacepy.lib.ulptr), surr_assoc_total.ctypes.data_as(spacepy.lib.ulptr), ctypes.c_ulong(n_lags), ctypes.c_ulong(n_p1), ctypes.c_ulong(n_surr), seeds.ctypes.data_as(spacepy.lib.ulptr), ctypes.c_int(0)) #This is just a regression test, really... expected = numpy.array([ 5, 2, 6, 1, 2, 6, 1, 4, 1, 9, 0, 3, 2, 3, 6, 2, 3, 9, 1, 4, 6, 1, 6, 0, 4, 6, 6, 6, 3, 3, 5, 3, 3, 0, 5, 0, 5, 5, 4, 6 ], dtype=dtype) numpy.testing.assert_array_equal(surr_assoc_total, expected)
def get_last_event(self): f = self._control._IDebugControl__com_GetLastEventInformation typ = ct.c_ulong() pid = ct.c_ulong() tid = ct.c_ulong() extra = ct.create_string_buffer(1024) desc = ct.create_string_buffer(1024) extra_used = ct.c_ulong() desc_used = ct.c_ulong() hresult = f( ct.byref(typ), ct.byref(pid), ct.byref(tid), ct.byref(extra), ct.sizeof(extra), ct.byref(extra_used), desc, ct.sizeof(desc), ct.byref(desc_used), ) if hresult != S_OK: raise RuntimeError("Ffuuuuuuuuuck: %d" % hresult) return (typ.value, pid.value, tid.value, extra.raw[: extra_used.value])
def select_windows(rlist, wlist, xlist, timeout, retry=True): """ on Windows, select() works only of sockets work of anonymous pipes is done as follows: PeekNamedPipe returns the amount of data avaialble. ReadFile calls with a higher size will be blocked so, in order to support size-limited reads, this function doesn't return list of file distriptors, but a list of tuples: (fd, bytes_available,) since IOLoop.do_iterations calls select with xlist=[], I do not iterate over xlist at all since PeekNamedPipe does not have a timeout, we will try run two iterations over all the fds in rlist """ from ctypes import windll, byref, c_ulong, c_void_p, WinError, GetLastError PIPE_ENDED = 109 read_ready = [] rlist = [item for item in rlist] for i in range(2): for fd in rlist: bytes_available = c_ulong(0) handle = _get_named_pipe_from_fileno(fd.fileno()) result = windll.kernel32.PeekNamedPipe(c_void_p(handle), 0, c_ulong(0), 0, byref(bytes_available), 0) if not result: last_error = GetLastError() if last_error != PIPE_ENDED: raise WinError(last_error) continue if bytes_available.value: read_ready.append((fd, bytes_available.value)) rlist.remove(fd) sleep(timeout) return read_ready, wlist, xlist
def set_kernel_process_name(name): """Changes the Kernel's /proc/self/status process name on Linux. The kernel name is NOT what will be shown by the ps or top command. It is a 15 character string stored in the kernel's process table that is included in the kernel log when a process is OOM killed. The first 15 bytes of name are used. Non-ASCII unicode is replaced with '?'. Does nothing if /proc/self/comm cannot be written or prctl() fails. Args: name: bytes|unicode, the Linux kernel's command name to set. """ if not isinstance(name, bytes): name = name.encode('ascii', 'replace') try: # This is preferred to using ctypes to try and call prctl() when possible. with open('/proc/self/comm', 'wb') as proc_comm: proc_comm.write(name[:15]) except EnvironmentError: try: import ctypes except ImportError: return # No ctypes. try: libc = ctypes.CDLL('libc.so.6') except EnvironmentError: return # No libc.so.6. pr_set_name = ctypes.c_ulong(15) # linux/prctl.h PR_SET_NAME value. zero = ctypes.c_ulong(0) try: libc.prctl(pr_set_name, name, zero, zero, zero) # Ignore the prctl return value. Nothing we can do if it errored. except AttributeError: return # No prctl.
def main(arg): with open("C:/Users/user/Documents/Visual Studio 2013/Projects/pCTF2015/forensics/150/test_crc_binary_second_byte.bin", "rb") as fi: data = fi.read() b = bytearray(data) for x in range(0, 256): # data[0] = 0x00 b[len(data) - 1] = x d = binascii.a2b_hex(binascii.hexlify(b[4:0x20004])) # calculate tag crc tag_crc = binascii.crc32(b[0:4]) item = ctypes.c_ulong(zlib.crc32(d, tag_crc)) item2 = ctypes.c_ulong(zlib.crc32(d)) # print "0x%s"%binascii.hexlify(d) final_crc = "%x" % item.value # if arg in final_crc: print "%s" % binascii.hexlify(d[len(d) - 4:len(d)]) print "final crc is 0x%x" % (item.value) # print "final crc without tag is 0x%x"%(item2.value) # print "%s"%binascii.hexlify(d[0x1fffc:0x20000]) # b.append(data) # print "0x%s"%binascii.hexlify(b) print "data length is 0x%x" % (len(data) - 4) pass
def get_process_list(): process_list = [] if os.name != "nt": return process_list Process = collections.namedtuple("Process", "filename name") PROCESS_QUERY_INFORMATION = 0x0400 PROCESS_VM_READ = 0x0010 lpidProcess= (ctypes.c_ulong * 1024)() cbNeeded = ctypes.c_ulong() ctypes.windll.psapi.EnumProcesses(ctypes.byref(lpidProcess), ctypes.sizeof(lpidProcess), ctypes.byref(cbNeeded)) nReturned = cbNeeded.value // ctypes.sizeof(cbNeeded) pidProcess = [i for i in lpidProcess][:nReturned] has_queryimage = hasattr(ctypes.windll.kernel32, "QueryFullProcessImageNameA") for pid in pidProcess: hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid) if hProcess: modname = ctypes.create_string_buffer(2048) count = ctypes.c_ulong(ctypes.sizeof(modname)) if has_queryimage: ctypes.windll.kernel32.QueryFullProcessImageNameA(hProcess, 0, ctypes.byref(modname), ctypes.byref(count)) else: ctypes.windll.psapi.GetModuleFileNameExA(hProcess, 0, ctypes.byref(modname), ctypes.byref(count)) path = modname.value.decode("mbcs") filename = os.path.basename(path) name, ext = os.path.splitext(filename) process_list.append(Process(filename=filename, name=name)) ctypes.windll.kernel32.CloseHandle(hProcess) return process_list
def write(self, data): data = bytes(data) assert len(data) % self.bytes_per_sample == 0 buf = ctypes.c_char_p(data) frames = ctypes.c_ulong(len(data) // self.bytes_per_sample) self.interface.call('WriteStream', self.stream, buf, frames)
kernel32 = windll.kernel32 GetStdHandle = kernel32.GetStdHandle ReadConsoleW = kernel32.ReadConsoleW WriteConsoleW = kernel32.WriteConsoleW GetLastError = kernel32.GetLastError GetConsoleCursorInfo = kernel32.GetConsoleCursorInfo SetConsoleCursorInfo = kernel32.SetConsoleCursorInfo GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32)) CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))( ("CommandLineToArgvW", windll.shell32) ) # XXX: Added for cursor hiding on windows STDOUT_HANDLE_ID = ctypes.c_ulong(-11) STDERR_HANDLE_ID = ctypes.c_ulong(-12) STDIN_HANDLE = GetStdHandle(-10) STDOUT_HANDLE = GetStdHandle(-11) STDERR_HANDLE = GetStdHandle(-12) STREAM_MAP = {0: STDIN_HANDLE, 1: STDOUT_HANDLE, 2: STDERR_HANDLE} PyBUF_SIMPLE = 0 PyBUF_WRITABLE = 1 ERROR_SUCCESS = 0 ERROR_NOT_ENOUGH_MEMORY = 8 ERROR_OPERATION_ABORTED = 995
def get_uint_info(self, field): length = ctypes.c_ulong() ret = xglib.XGDMatrixGetUIntInfo(self.handle, c_str(field), ctypes.byref(length)) return ctypes2numpy(ret, length.value, np.uint32)
def kernel_analyze(self): """zer0m0n kernel analysis """ log.info("Starting kernel analysis") log.info("Installing driver") if is_os_64bit(): sys_file = os.path.join(os.getcwd(), "dll", "zer0m0n_x64.sys") else: sys_file = os.path.join(os.getcwd(), "dll", "zer0m0n.sys") exe_file = os.path.join(os.getcwd(), "dll", "logs_dispatcher.exe") if not sys_file or not exe_file or not os.path.exists( sys_file) or not os.path.exists(exe_file): log.warning( "No valid zer0m0n files to be used for process with pid %d, injection aborted", self.pid) return False exe_name = random_string(6) service_name = random_string(6) driver_name = random_string(6) inf_data = '[Version]\r\nSignature = "$Windows NT$"\r\nClass = "ActivityMonitor"\r\nClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2}\r\nProvider= %Prov%\r\nDriverVer = 22/01/2014,1.0.0.0\r\nCatalogFile = %DriverName%.cat\r\n[DestinationDirs]\r\nDefaultDestDir = 12\r\nMiniFilter.DriverFiles = 12\r\n[DefaultInstall]\r\nOptionDesc = %ServiceDescription%\r\nCopyFiles = MiniFilter.DriverFiles\r\n[DefaultInstall.Services]\r\nAddService = %ServiceName%,,MiniFilter.Service\r\n[DefaultUninstall]\r\nDelFiles = MiniFilter.DriverFiles\r\n[DefaultUninstall.Services]\r\nDelService = %ServiceName%,0x200\r\n[MiniFilter.Service]\r\nDisplayName= %ServiceName%\r\nDescription= %ServiceDescription%\r\nServiceBinary= %12%\\%DriverName%.sys\r\nDependencies = "FltMgr"\r\nServiceType = 2\r\nStartType = 3\r\nErrorControl = 1\r\nLoadOrderGroup = "FSFilter Activity Monitor"\r\nAddReg = MiniFilter.AddRegistry\r\n[MiniFilter.AddRegistry]\r\nHKR,,"DebugFlags",0x00010001 ,0x0\r\nHKR,"Instances","DefaultInstance",0x00000000,%DefaultInstance%\r\nHKR,"Instances\\"%Instance1.Name%,"Altitude",0x00000000,%Instance1.Altitude%\r\nHKR,"Instances\\"%Instance1.Name%,"Flags",0x00010001,%Instance1.Flags%\r\n[MiniFilter.DriverFiles]\r\n%DriverName%.sys\r\n[SourceDisksFiles]\r\n' + driver_name + '.sys = 1,,\r\n[SourceDisksNames]\r\n1 = %DiskId1%,,,\r\n[Strings]\r\n' + 'Prov = "' + random_string( 8 ) + '"\r\nServiceDescription = "' + random_string( 12 ) + '"\r\nServiceName = "' + service_name + '"\r\nDriverName = "' + driver_name + '"\r\nDiskId1 = "' + service_name + ' Device Installation Disk"\r\nDefaultInstance = "' + service_name + ' Instance"\r\nInstance1.Name = "' + service_name + ' Instance"\r\nInstance1.Altitude = "370050"\r\nInstance1.Flags = 0x0' new_inf = os.path.join(os.getcwd(), "dll", "{0}.inf".format(service_name)) new_sys = os.path.join(os.getcwd(), "dll", "{0}.sys".format(driver_name)) copy(sys_file, new_sys) new_exe = os.path.join(os.getcwd(), "dll", "{0}.exe".format(exe_name)) copy(exe_file, new_exe) log.info("[-] Driver name : " + new_sys) log.info("[-] Inf name : " + new_inf) log.info("[-] Application name : " + new_exe) log.info("[-] Service : " + service_name) fh = open(new_inf, "w") fh.write(inf_data) fh.close() os_is_64bit = is_os_64bit() if os_is_64bit: wow64 = c_ulong(0) KERNEL32.Wow64DisableWow64FsRedirection(byref(wow64)) os.system( 'cmd /c "rundll32 setupapi.dll, InstallHinfSection DefaultInstall 132 ' + new_inf + '"') os.system("net start " + service_name) si = STARTUPINFO() si.cb = sizeof(si) pi = PROCESS_INFORMATION() cr = CREATE_NEW_CONSOLE ldp = KERNEL32.CreateProcessA(new_exe, None, None, None, None, cr, None, os.getenv("TEMP"), byref(si), byref(pi)) if not ldp: if os_is_64bit: KERNEL32.Wow64RevertWow64FsRedirection(wow64) log.error("Failed starting " + exe_name + ".exe.") return False config_path = os.path.join(os.getenv("TEMP"), "%s.ini" % self.pid) with open(config_path, "w") as config: cfg = Config("analysis.conf") config.write("host-ip={0}\n".format(cfg.ip)) config.write("host-port={0}\n".format(cfg.port)) config.write("pipe={0}\n".format(PIPE)) log.info("Sending startup information") hFile = KERNEL32.CreateFileA(PATH_KERNEL_DRIVER, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, None) if os_is_64bit: KERNEL32.Wow64RevertWow64FsRedirection(wow64) if hFile: p = Process(pid=os.getpid()) ppid = p.get_parent_pid() pid_vboxservice = 0 pid_vboxtray = 0 # get pid of VBoxService.exe and VBoxTray.exe proc_info = PROCESSENTRY32() proc_info.dwSize = sizeof(PROCESSENTRY32) snapshot = KERNEL32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) flag = KERNEL32.Process32First(snapshot, byref(proc_info)) while flag: if proc_info.sz_exeFile == "VBoxService.exe": log.info("VBoxService.exe found !") pid_vboxservice = proc_info.th32ProcessID flag = 0 elif proc_info.sz_exeFile == "VBoxTray.exe": pid_vboxtray = proc_info.th32ProcessID log.info("VBoxTray.exe found !") flag = 0 flag = KERNEL32.Process32Next(snapshot, byref(proc_info)) bytes_returned = c_ulong(0) msg = str(self.pid) + "_" + str(ppid) + "_" + str( os.getpid()) + "_" + str(pi.dwProcessId) + "_" + str( pid_vboxservice) + "_" + str(pid_vboxtray) + '\0' KERNEL32.DeviceIoControl(hFile, IOCTL_PID, msg, len(msg), None, 0, byref(bytes_returned), None) msg = os.getcwd() + '\0' KERNEL32.DeviceIoControl(hFile, IOCTL_CUCKOO_PATH, unicode(msg), len(unicode(msg)), None, 0, byref(bytes_returned), None) else: log.warning("Failed to access kernel driver") return True
# -*- encoding: utf-8 -*- # Copyright (c) 2017 Timo Savola. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. import collections import ctypes import os import resource import struct import subprocess ADDR_NO_RANDOMIZE = ctypes.c_ulong(0x0040000) GATE_LOADER_STACK_SIZE = 12288 # Must match the definition in runtime/include/runtime.h page = os.sysconf("SC_PAGESIZE") assert (GATE_LOADER_STACK_SIZE % page) == 0 libc = ctypes.CDLL("libc.so.6") def preexec(): ret = libc.personality(ADDR_NO_RANDOMIZE) if ret < 0: raise Exception(ret) resource.setrlimit(resource.RLIMIT_STACK, (GATE_LOADER_STACK_SIZE, GATE_LOADER_STACK_SIZE))
def PressKey(hexKeyCode): extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra)) x = Input(ctypes.c_ulong(1), ii_) ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def getVolumeInformation( self, volumeNameBuffer, volumeNameSize, volumeSerialNumber, maximumComponentLength, fileSystemFlags, fileSystemNameBuffer, fileSystemNameSize, dokanFileInfo, ): """Get information about the volume. :param volumeNameBuffer: buffer for volume name :type volumeNameBuffer: ctypes.c_void_p :param volumeNameSize: volume name buffer size :type volumeNameSize: ctypes.c_ulong :param volumeSerialNumber: buffer for volume serial number :type volumeSerialNumber: ctypes.c_void_p :param maximumComponentLength: buffer for maximum component length :type maximumComponentLength: ctypes.c_void_p :param fileSystemFlags: buffer for file system flags :type fileSystemFlags: ctypes.c_void_p :param fileSystemNameBuffer: buffer for file system name :type fileSystemNameBuffer: ctypes.c_void_p :param fileSystemNameSize: file system name buffer size :type fileSystemNameSize: ctypes.c_ulong :param dokanFileInfo: used by Dokan :type dokanFileInfo: PDOKAN_FILE_INFO :return: error code :rtype: ctypes.c_int """ ret = self.operations("getVolumeInformation") # populate volume name buffer ctypes.memmove( volumeNameBuffer, ret["volumeNameBuffer"], min( ctypes.sizeof(ctypes.c_wchar) * len(ret["volumeNameBuffer"]), volumeNameSize, ), ) # populate serial number buffer serialNum = ctypes.c_ulong(self.serialNumber) ctypes.memmove(volumeSerialNumber, ctypes.byref(serialNum), ctypes.sizeof(ctypes.c_ulong)) # populate max component length maxCompLen = ctypes.c_ulong(ret["maximumComponentLength"]) ctypes.memmove( maximumComponentLength, ctypes.byref(maxCompLen), ctypes.sizeof(ctypes.c_ulong), ) # populate filesystem flags buffer fsFlags = ctypes.c_ulong(ret["fileSystemFlags"]) ctypes.memmove(fileSystemFlags, ctypes.byref(fsFlags), ctypes.sizeof(ctypes.c_ulong)) # populate filesystem name ctypes.memmove( fileSystemNameBuffer, ret["fileSystemNameBuffer"], min( ctypes.sizeof(ctypes.c_wchar) * len(ret["fileSystemNameBuffer"]), fileSystemNameSize, ), ) return d1_onedrive.impl.drivers.dokan.const.DOKAN_SUCCESS
lpWindowName = ctypes.c_char_p( input("Enter Window Name To Hook Into: ").encode('utf-8')) # Grab a Handle to the Process hWnd = u_handle.FindWindowA(None, lpWindowName) # Check to see if we have the Handle if hWnd == 0: print("[ERROR] Could Not Grab Handle! Error Code: {0}".format( k_handle.GetLastError())) exit(1) else: print("[INFO] Grabbed Handle...") # Get the PID of the process at the handle lpdwProcessId = ctypes.c_ulong() # We use byref to pass a pointer to the value as needed by the API Call response = u_handle.GetWindowThreadProcessId(hWnd, ctypes.byref(lpdwProcessId)) # Check to see if the call Completed if response == 0: print("[ERROR] Could Not Get PID from Handle! Error Code: {0}".format( k_handle.GetLastError())) else: print("[INFO] Found PID...") # Opening the Process by PID with Specific Access dwDesiredAccess = PROCESS_ALL_ACCESS bInheritHandle = False dwProcessId = lpdwProcessId
def adsSyncReadWriteReqEx2( port: int, address: AmsAddr, index_group: int, index_offset: int, read_data_type: Optional[Type], value: Any, write_data_type: Optional[Type], return_ctypes: bool = False, check_length: bool = True, ) -> Any: """Read and write data synchronous from/to an ADS-device. :param int port: local AMS port as returned by adsPortOpenEx() :param pyads.structs.AmsAddr address: local or remote AmsAddr :param int index_group: PLC storage area, according to the INDEXGROUP constants :param int index_offset: PLC storage address :param Type read_data_type: type of the data given to the PLC to respond to, according to PLCTYPE constants, or None to not read anything :param value: value to write to the storage address of the PLC :param Type write_data_type: type of the data given to the PLC, according to PLCTYPE constants, or None to not write anything :param bool return_ctypes: return ctypes instead of python types if True (default: False) :param bool check_length: check whether the amount of bytes read matches the size of the read data type (default: True) :rtype: read_data_type :return: value: value read from PLC """ sync_read_write_request = _adsDLL.AdsSyncReadWriteReqEx2 ams_address_pointer = ctypes.pointer(address.amsAddrStruct()) index_group_c = ctypes.c_ulong(index_group) index_offset_c = ctypes.c_ulong(index_offset) read_data: Optional[Any] response_size: int = 0 if index_group == ADSIGRP_SUMUP_READ: response_size = ctypes.sizeof(ctypes.c_ulong) * len(value) for _ in value: response_size += _.size read_data_buf = bytearray(response_size) read_data = (ctypes.c_byte * len(read_data_buf)).from_buffer(read_data_buf) read_data_pointer = ctypes.pointer(read_data) read_length = response_size elif index_group == ADSIGRP_SUMUP_WRITE: response_size = ( index_offset * 4 ) # expect 4 bytes back for every value written (error data) read_data_buf = bytearray(response_size) read_data = (ctypes.c_byte * len(read_data_buf)).from_buffer(read_data_buf) read_data_pointer = ctypes.pointer(read_data) read_length = response_size elif read_data_type is None: read_data = None read_data_pointer = None read_length = ctypes.c_ulong(0) else: if read_data_type == PLCTYPE_STRING: read_data = (STRING_BUFFER * PLCTYPE_STRING)() else: read_data = read_data_type() read_data_pointer = ctypes.pointer(read_data) read_length = ctypes.c_ulong(ctypes.sizeof(read_data)) bytes_read = ctypes.c_ulong() bytes_read_pointer = ctypes.pointer(bytes_read) write_data_pointer: Optional[Union[ctypes.c_char_p, ctypes.pointer]] if index_group == ADSIGRP_SUMUP_READ: write_data_pointer = ctypes.pointer(value) write_length = ctypes.sizeof(value) elif index_group == ADSIGRP_SUMUP_WRITE: write_data = (ctypes.c_byte * len(value)).from_buffer(value) write_data_pointer = ctypes.pointer(write_data) write_length = ctypes.sizeof(write_data) elif write_data_type is None: write_data_pointer = None write_length = ctypes.c_ulong(0) elif write_data_type == PLCTYPE_STRING: # Get pointer to string write_data_pointer = ctypes.c_char_p(value.encode("utf-8")) # Add an extra byte to the data length for the null terminator write_length = ctypes.c_ulong(len(value) + 1) else: if type(write_data_type).__name__ == "PyCArrayType": write_data = write_data_type(*value) elif type(value) is write_data_type: write_data = value else: write_data = write_data_type(value) write_data_pointer = ctypes.pointer(write_data) write_length = ctypes.c_ulong(ctypes.sizeof(write_data)) err_code = sync_read_write_request( port, ams_address_pointer, index_group_c, index_offset_c, read_length, read_data_pointer, write_length, write_data_pointer, bytes_read_pointer, ) if err_code: raise ADSError(err_code) if index_group == ADSIGRP_SUMUP_READ or index_group == ADSIGRP_SUMUP_WRITE: expected_length = response_size else: expected_length = (read_data.entryLength if isinstance( read_data, SAdsSymbolEntry) else read_length.value) # If we're reading a value of predetermined size (anything but a string), # validate that the correct number of bytes were read if (check_length and read_data_type != PLCTYPE_STRING and bytes_read.value != expected_length): raise RuntimeError( "Insufficient data (expected {0} bytes, {1} were read).".format( expected_length, bytes_read.value)) if return_ctypes: return read_data if read_data is not None and read_data_type == PLCTYPE_STRING: return read_data.value.decode("utf-8") if read_data is not None and type( read_data_type).__name__ == "PyCArrayType": return [i for i in read_data] if read_data is not None and hasattr(read_data, "value"): return read_data.value return read_data
if __name__ == '__main__': # maxInt = sys.maxsize # while True: # # decrease the maxInt value by factor 10 # # as long as the OverflowError occurs. # try: # csv.field_size_limit(maxInt) # break # except OverflowError: # maxInt = int(maxInt/10) import ctypes csv.field_size_limit(int(ctypes.c_ulong(-1).value // 2)) transformer = NLXTransformer(s3_prefix='open-skills-private/NLX_extracted', temp_file_path='/mnt/sqltransfer') logging.basicConfig(level=logging.INFO) #logging.info('max csv size set to {}'.format(maxInt)) #for year in ('2003', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2016', '2017', '2018'): #for year in ('2003', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013'): #for year in ('2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013'): for year in range(2003, 2020): year = str(year) #stats_counter = DatasetStatsCounter( # quarter=year, # dataset_id='NLX' #)
def tounsigned_long(var): return ctypes.c_ulong(var).value
def adsSyncReadReqEx2( port: int, address: AmsAddr, index_group: int, index_offset: int, data_type: Type, return_ctypes: bool = False, check_length: bool = True, ) -> Any: """Read data synchronous from an ADS-device. :param int port: local AMS port as returned by adsPortOpenEx() :param pyads.structs.AmsAddr address: local or remote AmsAddr :param int index_group: PLC storage area, according to the INDEXGROUP constants :param int index_offset: PLC storage address :param Type data_type: type of the data given to the PLC, according to PLCTYPE constants :param bool return_ctypes: return ctypes instead of python types if True (default: False) :param bool check_length: check whether the amount of bytes read matches the size of the read data type (default: True) :rtype: data_type :return: value: **value** """ sync_read_request = _adsDLL.AdsSyncReadReqEx2 ams_address_pointer = ctypes.pointer(address.amsAddrStruct()) index_group_c = ctypes.c_ulong(index_group) index_offset_c = ctypes.c_ulong(index_offset) if data_type == PLCTYPE_STRING: data = (STRING_BUFFER * PLCTYPE_STRING)() else: data = data_type() data_pointer = ctypes.pointer(data) data_length = ctypes.c_ulong(ctypes.sizeof(data)) bytes_read = ctypes.c_ulong() bytes_read_pointer = ctypes.pointer(bytes_read) error_code = sync_read_request( port, ams_address_pointer, index_group_c, index_offset_c, data_length, data_pointer, bytes_read_pointer, ) if error_code: raise ADSError(error_code) # If we're reading a value of predetermined size (anything but a string), # validate that the correct number of bytes were read if (check_length and data_type != PLCTYPE_STRING and bytes_read.value != data_length.value): raise RuntimeError( "Insufficient data (expected {0} bytes, {1} were read).".format( data_length.value, bytes_read.value)) if return_ctypes: return data if data_type == PLCTYPE_STRING: return data.value.decode("utf-8") if type(data_type).__name__ == "PyCArrayType": return [i for i in data] if hasattr(data, "value"): return data.value return data
def devices(guid=None, enumerator=None, hwnd_parent=None, flags=DIGCF_PRESENT): """ Return generator of `Device` objects which is specified by given parameters Parameters are almost same to SetupDiGetClassDev's :param guid: A string that describes the GUID for a device setup class or a device interface class. The format of GUID string is same to CLSIDFromString. :param enumerator: An ID of a PnP enumerator. :param hwnd_parent: A handle to the top-level window. This handle is optional and can be None :param flags: A variable of type DWORD that specifies control options. :return: Iterable of Device objects """ if guid is None: guid_ = None flags |= DIGCF_ALLCLASSES elif isinstance(guid, GUID): guid_ = bytes(guid) else: guid_ = bytes(GUID(str(guid))) if enumerator is None: enumerator_ = None else: enumerator_ = ctypes.create_unicode_buffer(enumerator) handle = _setupapi.SetupDiGetClassDevsW(guid_, enumerator_, hwnd_parent, flags) if handle == -1: err_no = ctypes.GetLastError() raise WindowsError(err_no, ctypes.FormatError(err_no), (guid, enumerator, flags)) try: idx = 0 dev_info = DeviceInfoData() while _setupapi.SetupDiEnumDeviceInfo(handle, idx, ctypes.byref(dev_info)): idx += 1 prop_type = ctypes.c_ulong() required_size = ctypes.c_ulong() instance_id = None if not _setupapi.SetupDiGetDevicePropertyW( handle, ctypes.byref(dev_info), ctypes.byref(DEVPKEY.Device.InstanceId), ctypes.byref(prop_type), None, 0, ctypes.byref(required_size), 0): err_no = ctypes.GetLastError() if err_no == 122: # ERROR_INSUFFICIENT_BUFFER instance_id_buffer = ctypes.create_string_buffer( required_size.value) if _setupapi.SetupDiGetDevicePropertyW( handle, ctypes.byref(dev_info), ctypes.byref(DEVPKEY.Device.InstanceId), ctypes.byref(prop_type), ctypes.byref(instance_id_buffer), required_size.value, ctypes.byref(required_size), 0): instance_id = Property(bytes(instance_id_buffer), prop_type.value) err_no = ctypes.GetLastError() if err_no != 0: raise WindowsError(err_no, ctypes.FormatError(err_no)) yield Device(instance_id) err_no = ctypes.GetLastError() if err_no != 259: raise WindowsError(err_no, ctypes.FormatError(err_no), (guid, enumerator, flags)) finally: _setupapi.SetupDiDestroyDeviceInfoList(handle)
def adsSyncAddDeviceNotificationReqEx( port: int, adr: AmsAddr, data: Union[str, Tuple[int, int]], pNoteAttrib: NotificationAttrib, callback: Callable, user_handle: Optional[int] = None, ) -> Tuple[int, int]: """Add a device notification. :param int port: local AMS port as returned by adsPortOpenEx() :param pyads.structs.AmsAddr adr: local or remote AmsAddr :param Union[str, Tuple[int, int]] data: PLC storage address by name or index group and offset :param pyads.structs.NotificationAttrib pNoteAttrib: notification attributes :param callback: Callback function to handle notification :param user_handle: User Handle :rtype: (int, int) :returns: notification handle, user handle """ global callback_store if NOTEFUNC is None: raise TypeError("Callback function type can't be None") adsSyncAddDeviceNotificationReqFct = _adsDLL.AdsSyncAddDeviceNotificationReqEx pAmsAddr = ctypes.pointer(adr.amsAddrStruct()) if isinstance(data, str): hnl = adsSyncReadWriteReqEx2(port, adr, ADSIGRP_SYM_HNDBYNAME, 0x0, PLCTYPE_UDINT, data, PLCTYPE_STRING) nIndexGroup = ctypes.c_ulong(ADSIGRP_SYM_VALBYHND) nIndexOffset = ctypes.c_ulong(hnl) elif isinstance(data, tuple): nIndexGroup = ctypes.c_ulong(data[0]) nIndexOffset = ctypes.c_ulong(data[1]) hnl = None else: raise TypeError( "Parameter data has the wrong type %s. Allowed types are: str, Tuple[int, int]." % (type(data))) attrib = pNoteAttrib.notificationAttribStruct() pNotification = ctypes.c_ulong() nHUser = ctypes.c_ulong(0) if hnl is not None: nHUser = ctypes.c_ulong(hnl) if user_handle is not None: nHUser = ctypes.c_ulong(user_handle) adsSyncAddDeviceNotificationReqFct.argtypes = [ ctypes.c_ulong, ctypes.POINTER(SAmsAddr), ctypes.c_ulong, ctypes.c_ulong, ctypes.POINTER(SAdsNotificationAttrib), NOTEFUNC, # type: ignore ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ] adsSyncAddDeviceNotificationReqFct.restype = ctypes.c_long def wrapper(addr, notification, user): # type: (AmsAddr, SAdsNotificationHeader, int) -> Callable[[SAdsNotificationHeader, str], None] return callback(notification, data) c_callback = NOTEFUNC(wrapper) err_code = adsSyncAddDeviceNotificationReqFct( port, pAmsAddr, nIndexGroup, nIndexOffset, ctypes.byref(attrib), c_callback, nHUser, ctypes.byref(pNotification), ) if err_code: raise ADSError(err_code) callback_store[(adr, pNotification.value)] = c_callback return pNotification.value, hnl
def a_in_scan_read(self, samples_per_channel, timeout): # pylint: disable=too-many-locals """ Read scan status and data (as a list). The analog input scan is started with :py:func:`a_in_scan_start` and runs in the background. This function reads the status of that background scan and optionally reads sampled data from the scan buffer. Args: samples_per_channel (int): The number of samples per channel to read from the scan buffer. Specify a negative number to return all available samples immediately and ignore **timeout** or 0 to only read the scan status and return no data. timeout (float): The amount of time in seconds to wait for the samples to be read. Specify a negative number to wait indefinitely, or 0 to return immediately with the samples that are already in the scan buffer (up to **samples_per_channel**.) If the timeout is met and the specified number of samples have not been read, then the function will return all the available samples and the timeout status set. Returns: namedtuple: a namedtuple containing the following field names: * **running** (bool): True if the scan is running, False if it has stopped or completed. * **hardware_overrun** (bool): True if the hardware could not acquire and unload samples fast enough and data was lost. * **buffer_overrun** (bool): True if the background scan buffer was not read fast enough and data was lost. * **triggered** (bool): True if the trigger conditions have been met and data acquisition started. * **timeout** (bool): True if the timeout time expired before the specified number of samples were read. * **data** (list of float): The data that was read from the scan buffer. Raises: HatError: A scan is not active, the board is not initialized, does not respond, or responds incorrectly. ValueError: Incorrect argument. """ if not self._initialized: raise HatError(self._address, "Not initialized.") num_channels = self._lib.mcc118_a_in_scan_channel_count(self._address) self._lib.mcc118_a_in_scan_read.argtypes = [ c_ubyte, POINTER(c_ushort), c_long, c_double, POINTER(c_double), c_ulong, POINTER(c_ulong) ] samples_read_per_channel = c_ulong(0) samples_to_read = 0 status = c_ushort(0) timed_out = False if samples_per_channel < 0: # read all available data # first, get the number of available samples samples_available = c_ulong(0) result = self._lib.mcc118_a_in_scan_status( self._address, byref(status), byref(samples_available)) if result != self._RESULT_SUCCESS: raise HatError(self._address, "Incorrect response {}.".format(result)) # allocate a buffer large enough for all the data samples_to_read = samples_available.value buffer_size = samples_available.value * num_channels data_buffer = (c_double * buffer_size)() elif samples_per_channel == 0: # only read the status samples_to_read = 0 buffer_size = 0 data_buffer = None elif samples_per_channel > 0: # read the specified number of samples samples_to_read = samples_per_channel # create a C buffer for the read buffer_size = samples_per_channel * num_channels data_buffer = (c_double * buffer_size)() else: # invalid samples_per_channel raise ValueError( "Invalid samples_per_channel {}.".format(samples_per_channel)) result = self._lib.mcc118_a_in_scan_read( self._address, byref(status), samples_to_read, timeout, data_buffer, buffer_size, byref(samples_read_per_channel)) if result == self._RESULT_BAD_PARAMETER: raise ValueError("Invalid parameter.") elif result == self._RESULT_RESOURCE_UNAVAIL: raise HatError(self._address, "Scan not active.") elif result == self._RESULT_TIMEOUT: timed_out = True elif result != self._RESULT_SUCCESS: raise HatError(self._address, "Incorrect response {}.".format(result)) total_read = samples_read_per_channel.value * num_channels # python 2 / 3 workaround for xrange if sys.version_info.major > 2: data_list = [data_buffer[i] for i in range(total_read)] else: data_list = [data_buffer[i] for i in xrange(total_read)] scan_status = namedtuple('MCC118ScanRead', [ 'running', 'hardware_overrun', 'buffer_overrun', 'triggered', 'timeout', 'data' ]) return scan_status( running=(status.value & self._STATUS_RUNNING) != 0, hardware_overrun=(status.value & self._STATUS_HW_OVERRUN) != 0, buffer_overrun=(status.value & self._STATUS_BUFFER_OVERRUN) != 0, triggered=(status.value & self._STATUS_TRIGGERED) != 0, timeout=timed_out, data=data_list)
def test_get_cluster_notify_v2(self): fake_notif_key = 1 fake_notif_port_h = 2 fake_notif_type = 3 fake_filter_flags = 4 fake_clus_obj_name = 'fake-changed-clus-object' fake_event_buff = 'fake-event-buff' fake_obj_type = 'fake-object-type' fake_obj_id = 'fake-obj-id' fake_parent_id = 'fake-parent-id' notif_key = ctypes.c_ulong(fake_notif_key) requested_buff_sz = 1024 def fake_get_cluster_notify(func, notif_port_h, pp_notif_key, p_filter_and_type, p_buff, p_buff_sz, p_obj_id_buff, p_obj_id_buff_sz, p_parent_id_buff, p_parent_id_buff_sz, p_obj_name_buff, p_obj_name_buff_sz, p_obj_type, p_obj_type_sz, timeout_ms): self.assertEqual(self._clusapi.GetClusterNotifyV2, func) self.assertEqual(fake_notif_port_h, notif_port_h) obj_name_buff_sz = ctypes.cast( p_obj_name_buff_sz, wintypes.PDWORD).contents buff_sz = ctypes.cast( p_buff_sz, wintypes.PDWORD).contents obj_type_sz = ctypes.cast( p_obj_type_sz, wintypes.PDWORD).contents obj_id_sz = ctypes.cast( p_obj_id_buff_sz, wintypes.PDWORD).contents parent_id_buff_sz = ctypes.cast( p_parent_id_buff_sz, wintypes.PDWORD).contents # We'll just request the tested method to pass us # a buffer this large. if (buff_sz.value < requested_buff_sz or obj_name_buff_sz.value < requested_buff_sz or parent_id_buff_sz.value < requested_buff_sz or obj_type_sz.value < requested_buff_sz or obj_id_sz.value < requested_buff_sz): buff_sz.value = requested_buff_sz obj_name_buff_sz.value = requested_buff_sz parent_id_buff_sz.value = requested_buff_sz obj_type_sz.value = requested_buff_sz obj_id_sz.value = requested_buff_sz raise exceptions.ClusterWin32Exception( error_code=w_const.ERROR_MORE_DATA, func_name='GetClusterNotifyV2', error_message='error more data') pp_notif_key = ctypes.cast(pp_notif_key, ctypes.c_void_p) p_notif_key = ctypes.c_void_p.from_address(pp_notif_key.value) p_notif_key.value = ctypes.addressof(notif_key) filter_and_type = ctypes.cast( p_filter_and_type, ctypes.POINTER(clusapi_def.NOTIFY_FILTER_AND_TYPE)).contents filter_and_type.dwObjectType = fake_notif_type filter_and_type.FilterFlags = fake_filter_flags def set_wchar_buff(p_wchar_buff, wchar_buff_sz, value): wchar_buff = ctypes.cast( p_wchar_buff, ctypes.POINTER( ctypes.c_wchar * (wchar_buff_sz // ctypes.sizeof(ctypes.c_wchar)))) wchar_buff = wchar_buff.contents ctypes.memset(wchar_buff, 0, wchar_buff_sz) wchar_buff.value = value return wchar_buff set_wchar_buff(p_obj_name_buff, requested_buff_sz, fake_clus_obj_name) set_wchar_buff(p_buff, requested_buff_sz, fake_event_buff) set_wchar_buff(p_parent_id_buff, requested_buff_sz, fake_parent_id) set_wchar_buff(p_obj_type, requested_buff_sz, fake_obj_type) set_wchar_buff(p_obj_id_buff, requested_buff_sz, fake_obj_id) self.assertEqual(mock.sentinel.timeout_ms, timeout_ms) self._mock_run.side_effect = fake_get_cluster_notify event = self._clusapi_utils.get_cluster_notify_v2( fake_notif_port_h, mock.sentinel.timeout_ms) w_event_buff = ctypes.cast( event['buff'], ctypes.POINTER( ctypes.c_wchar * (requested_buff_sz // ctypes.sizeof(ctypes.c_wchar)))) w_event_buff = w_event_buff.contents[:] event['buff'] = w_event_buff.split('\x00')[0] expected_event = dict(cluster_object_name=fake_clus_obj_name, object_id=fake_obj_id, object_type=fake_notif_type, object_type_str=fake_obj_type, filter_flags=fake_filter_flags, parent_id=fake_parent_id, buff=fake_event_buff, buff_sz=requested_buff_sz, notif_key=fake_notif_key) self.assertEqual(expected_event, event)
# Copyright (c) 2017 Timo Savola. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. from collections import defaultdict from ctypes import CDLL, c_ulong from os import sysconf from resource import RLIMIT_STACK, setrlimit from struct import unpack from subprocess import PIPE, Popen from sys import argv ADDR_NO_RANDOMIZE = c_ulong(0x0040000) GATE_LOADER_STACK_SIZE = 12288 # Must match the definition in runtime/include/runtime.hpp page = sysconf("SC_PAGESIZE") assert (GATE_LOADER_STACK_SIZE % page) == 0 libc = CDLL("libc.so.6") def preexec(): ret = libc.personality(ADDR_NO_RANDOMIZE) if ret < 0: raise Exception(ret) setrlimit(RLIMIT_STACK, (GATE_LOADER_STACK_SIZE, GATE_LOADER_STACK_SIZE)) min_alloc = None
def a_in_scan_read_numpy(self, samples_per_channel, timeout): # pylint: disable=too-many-locals """ Read scan status and data (as a NumPy array). This function is similar to :py:func:`a_in_scan_read` except that the *data* key in the returned namedtuple is a NumPy array of float64 values and may be used directly with NumPy functions. Args: samples_per_channel (int): The number of samples per channel to read from the scan buffer. Specify a negative number to read all available samples or 0 to only read the scan status and return no data. timeout (float): The amount of time in seconds to wait for the samples to be read. Specify a negative number to wait indefinitely, or 0 to return immediately with the samples that are already in the scan buffer. If the timeout is met and the specified number of samples have not been read, then the function will return with the amount that has been read and the timeout status set. Returns: namedtuple: a namedtuple containing the following field names: * **running** (bool): True if the scan is running, False if it has stopped or completed. * **hardware_overrun** (bool): True if the hardware could not acquire and unload samples fast enough and data was lost. * **buffer_overrun** (bool): True if the background scan buffer was not read fast enough and data was lost. * **triggered** (bool): True if the trigger conditions have been met and data acquisition started. * **timeout** (bool): True if the timeout time expired before the specified number of samples were read. * **data** (NumPy array of float64): The data that was read from the scan buffer. Raises: HatError: A scan is not active, the board is not initialized, does not respond, or responds incorrectly. ValueError: Incorrect argument. """ try: import numpy from numpy.ctypeslib import ndpointer except ImportError: raise if not self._initialized: raise HatError(self._address, "Not initialized.") self._lib.mcc118_a_in_scan_read.argtypes = [ c_ubyte, POINTER(c_ushort), c_long, c_double, ndpointer(c_double, flags="C_CONTIGUOUS"), c_ulong, POINTER(c_ulong) ] num_channels = self._lib.mcc118_a_in_scan_channel_count(self._address) samples_read_per_channel = c_ulong() status = c_ushort() timed_out = False samples_to_read = 0 if samples_per_channel < 0: # read all available data # first, get the number of available samples samples_available = c_ulong(0) result = self._lib.mcc118_a_in_scan_status( self._address, byref(status), byref(samples_available)) if result != self._RESULT_SUCCESS: raise HatError(self._address, "Incorrect response {}.".format(result)) # allocate a buffer large enough for all the data samples_to_read = samples_available.value buffer_size = samples_available.value * num_channels data_buffer = numpy.empty(buffer_size, dtype=numpy.float64) elif samples_per_channel == 0: # only read the status samples_to_read = 0 buffer_size = 0 data_buffer = None elif samples_per_channel > 0: # read the specified number of samples samples_to_read = samples_per_channel # create a C buffer for the read buffer_size = samples_per_channel * num_channels data_buffer = numpy.empty(buffer_size, dtype=numpy.float64) else: # invalid samples_per_channel raise ValueError( "Invalid samples_per_channel {}.".format(samples_per_channel)) result = self._lib.mcc118_a_in_scan_read( self._address, byref(status), samples_to_read, timeout, data_buffer, buffer_size, byref(samples_read_per_channel)) if result == self._RESULT_BAD_PARAMETER: raise ValueError("Invalid parameter.") elif result == self._RESULT_RESOURCE_UNAVAIL: raise HatError(self._address, "Scan not active.") elif result == self._RESULT_TIMEOUT: timed_out = True elif result != self._RESULT_SUCCESS: raise HatError(self._address, "Incorrect response {}.".format(result)) total_read = samples_read_per_channel.value * num_channels if total_read < buffer_size: data_buffer = numpy.resize(data_buffer, (total_read, )) scan_status = namedtuple('MCC118ScanRead', [ 'running', 'hardware_overrun', 'buffer_overrun', 'triggered', 'timeout', 'data' ]) return scan_status( running=(status.value & self._STATUS_RUNNING) != 0, hardware_overrun=(status.value & self._STATUS_HW_OVERRUN) != 0, buffer_overrun=(status.value & self._STATUS_BUFFER_OVERRUN) != 0, triggered=(status.value & self._STATUS_TRIGGERED) != 0, timeout=timed_out, data=data_buffer)
def __init__(self, ao_chan, co_dev, ci_dev, ci_port, ao_range=(-10, 10), duty_cycle=0.96): ao_task = ctypes.c_ulong() # analog out co_task = ctypes.c_ulong() # finite pulse train ci_task = ctypes.c_ulong() # count photons CHK(dll.DAQmxCreateTask('', ctypes.byref(ao_task))) CHK(dll.DAQmxCreateTask('', ctypes.byref(co_task))) CHK(dll.DAQmxCreateTask('', ctypes.byref(ci_task))) # ni task for analog out CHK( dll.DAQmxCreateAOVoltageChan(ao_task, ao_chan, '', ctypes.c_double(ao_range[0]), ctypes.c_double(ao_range[1]), DAQmx_Val_Volts, '')) CHK( dll.DAQmxCreateCOPulseChanFreq( co_task, co_dev, '', DAQmx_Val_Hz, DAQmx_Val_Low, ctypes.c_double(0), # initial delay ctypes.c_double(1000), # frequency ctypes.c_double(duty_cycle))) CHK( dll.DAQmxCreateCIPulseWidthChan( ci_task, ci_dev, '', ctypes.c_double(0), # expected min ctypes.c_double(10000.), # expected max DAQmx_Val_Ticks, DAQmx_Val_Rising, '')) """ CHK( dll.DAQmxCreateCICountEdgesChan(ci_task, ci_dev, '', DAQmx_Val_Rising, 0, # initial count DAQmx_Val_CountUp )) """ CHK( dll.DAQmxSetCICountEdgesTerm(ci_task, ci_dev, co_dev + 'InternalOutput')) CHK(dll.DAQmxSetCICtrTimebaseSrc(ci_task, ci_dev, ci_port)) # read samples from beginning of acquisition, do not overwrite CHK(dll.DAQmxSetReadRelativeTo(ci_task, DAQmx_Val_CurrReadPos)) CHK(dll.DAQmxSetReadOffset(ci_task, 0)) CHK( dll.DAQmxSetReadOverWrite(ci_task, DAQmx_Val_DoNotOverwriteUnreadSamps)) self.ao_task = ao_task self.co_task = co_task self.ci_task = ci_task self.co_dev = co_dev self.duty_cycle = duty_cycle
interpolate = interpolateProto(("interpolate64", lib)) interpolatefProto = ctypes.WINFUNCTYPE( VecResult, # Return type. ctypes.c_void_p, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_ulong) interpolatef = interpolatefProto(("interpolatef64", lib)) vec = new64(ctypes.c_int(0), ctypes.c_int(0), ctypes.c_double(0.0), ctypes.c_ulong(20), ctypes.c_double(1.0)) num = 20 x = numpy.linspace(0, 30, num=num, endpoint=False) y1 = numpy.cos(-x * 2 / 6.0) y4 = signal.resample(y1, 5 * num) for i in range(0, num - 1): setValue(vec, i, y1[i]) vec_res = interpolate(vec, 0, 0, 5 * num, 0.5) assert (vec_res.resultCode == 0) vec = vec_res.result y2 = [0.0] * 5 * num for i in range(0, 5 * num - 1): y2[i] = getValue(vec, i)
def GetHist(tag_name, start_time, end_time, period=5, mode="raw", desc_as_label=False, label=None, high_speed=False, utc=False): """ Retrieves data from eDNA history for a given tag. :param tag_name: fully-qualified (site.service.tag) eDNA tag :param start_time: must be in format mm/dd/yy hh:mm:ss :param end_time: must be in format mm/dd/yy hh:mm:ss :param period: specify the number of seconds for the pull interval :param mode: "raw", "snap", "avg", "interp", "max", "min" See eDNA documentation for more information. :param desc_as_label: use the tag description as the column name instead of the full tag :param label: supply a custom label to use as the DataFrame column name :param high_speed: if True, pull millisecond data :param utc: if True, use the integer time format instead of DateTime :return: a pandas DataFrame with timestamp, value, and status """ # Check if the point even exists if not DoesIDExist(tag_name): warnings.warn("WARNING- " + tag_name + " does not exist or " + "connection was dropped. Try again if tag does exist.") return pd.DataFrame() # Define all required variables in the correct ctypes format szPoint = c_char_p(tag_name.encode('utf-8')) tStart = c_long(StringToUTCTime(start_time)) tEnd = c_long(StringToUTCTime(end_time)) tPeriod = c_long(period) pulKey = c_ulong(0) # Initialize the data pull using the specified pulKey, which is an # identifier that tells eDNA which data pull is occurring mode = mode.lower().strip() if not high_speed: if mode == "avg": nRet = dna_dll.DnaGetHistAvgUTC(szPoint, tStart, tEnd, tPeriod, byref(pulKey)) if mode == "interp": nRet = dna_dll.DnaGetHistInterpUTC(szPoint, tStart, tEnd, tPeriod, byref(pulKey)) if mode == "min": nRet = dna_dll.DnaGetHistMinUTC(szPoint, tStart, tEnd, tPeriod, byref(pulKey)) if mode == "max": nRet = dna_dll.DnaGetHistMaxUTC(szPoint, tStart, tEnd, tPeriod, byref(pulKey)) if mode == "snap": nRet = dna_dll.DnaGetHistSnapUTC(szPoint, tStart, tEnd, tPeriod, byref(pulKey)) else: nRet = dna_dll.DnaGetHistRawUTC(szPoint, tStart, tEnd, byref(pulKey)) time_, val, stat = _GetNextHistSmallUTC(pulKey, nRet) else: nStartMillis = c_ushort(0) nEndMillis = c_ushort(0) nRet = dna_dll.DnaGetHSHistRawUTC(szPoint, tStart, nStartMillis, tEnd, nEndMillis, byref(pulKey)) time_, val, stat = _GetNextHSHistUTC(pulKey, nRet) # The history request must be cancelled to free up network resources dna_dll.DnaCancelHistRequest(pulKey) # To construct the pandas DataFrame, the tag name will be used as the # column name, and the index (which is in the strange eDNA format) must be # converted to an actual DateTime d = {tag_name + ' Status': stat, tag_name: val} df = pd.DataFrame(data=d, index=time_) if not utc: if not high_speed: df.index = pd.to_datetime(df.index, unit="s") else: df.index = pd.to_datetime(df.index, unit="ms") if df.empty: warnings.warn('WARNING- No data retrieved for ' + tag_name + '. ' + 'Check eDNA connection, ensure that the start time is ' + 'not later than the end time, verify that the ' + 'DateTime formatting matches eDNA requirements, and ' + 'check that data exists in the query time period.') # Check if the user would rather use the description as the column name if desc_as_label or label: if label: new_label = label else: new_label = _GetLabel(tag_name) df.rename(inplace=True, columns={ tag_name: new_label, tag_name + " Status": new_label + " Status" }) return df
def create_surface(self, x=0, y=0, w=0, h=0, layer=0): #Set the viewport position and size dst_rect = c_ints((x, y, w, h)) src_rect = c_ints((x, y, w << 16, h << 16)) if PLATFORM == PLATFORM_ANDROID: self.surface = openegl.eglGetCurrentSurface(EGL_DRAW) # Get the width and height of the screen - TODO, this system returns 100x100 time.sleep(0.2) #give it a chance to find out the dimensions w = c_int() h = c_int() openegl.eglQuerySurface(self.display, self.surface, EGL_WIDTH, byref(w)) openegl.eglQuerySurface(self.display, self.surface, EGL_HEIGHT, byref(h)) self.width, self.height = w.value, h.value elif PLATFORM == PLATFORM_PI: self.dispman_display = bcm.vc_dispmanx_display_open( 0) #LCD setting self.dispman_update = bcm.vc_dispmanx_update_start(0) alpha = c_ints((DISPMANX_FLAGS_ALPHA_PREMULT, 0, 0)) self.dispman_element = bcm.vc_dispmanx_element_add( self.dispman_update, self.dispman_display, layer, dst_rect, 0, src_rect, DISPMANX_PROTECTION_NONE, alpha, 0, 0) nativewindow = (GLint * 3)(self.dispman_element, w, h + 1) bcm.vc_dispmanx_update_submit_sync(self.dispman_update) self.nw_p = ctypes.pointer(nativewindow) ### NB changing the argtypes to allow passing of bcm native window is ### deeply unsatisfactory. But xlib defines Window as c_ulong and ctypes ### isn't happy about a pointer being cast to an int openegl.eglCreateWindowSurface.argtypes = [ EGLDisplay, EGLConfig, POINTER((GLint * 3)), EGLint ] self.surface = openegl.eglCreateWindowSurface( self.display, self.config, self.nw_p, 0) elif pi3d.USE_PYGAME: import pygame flags = pygame.OPENGL wsize = (w, h) if w == self.width and h == self.height: # i.e. full screen flags = pygame.FULLSCREEN | pygame.OPENGL wsize = (0, 0) if self.display_config & DISPLAY_CONFIG_NO_RESIZE: flags |= pygame.RESIZABLE if self.display_config & DISPLAY_CONFIG_NO_FRAME: flags |= pygame.NOFRAME if self.display_config & DISPLAY_CONFIG_FULLSCREEN: flags |= pygame.FULLSCREEN elif self.display_config & DISPLAY_CONFIG_MAXIMIZED: flags |= pygame.FULLSCREEN wsize = (0, 0) self.width, self.height = w, h self.d = pygame.display.set_mode(wsize, flags) self.window = pygame.display.get_wm_info()["window"] self.surface = openegl.eglCreateWindowSurface( self.display, self.config, self.window, 0) else: # work on basis it's X11 # Set some WM info self.root = xlib.XRootWindowOfScreen(self.screen) if self.use_glx: # For drawing on X window with transparent background numfbconfigs = c_int() VisData = c_ints( (glx.GLX_RENDER_TYPE, glx.GLX_RGBA_BIT, glx.GLX_DRAWABLE_TYPE, glx.GLX_WINDOW_BIT, glx.GLX_DOUBLEBUFFER, True, glx.GLX_RED_SIZE, 8, glx.GLX_GREEN_SIZE, 8, glx.GLX_BLUE_SIZE, 8, glx.GLX_ALPHA_SIZE, 8, glx.GLX_DEPTH_SIZE, 16, 0)) glx_screen = xlib.XDefaultScreen(self.d) fbconfigs = glx.glXChooseFBConfig(self.d, glx_screen, VisData, byref(numfbconfigs)) fbconfig = 0 for i in range(numfbconfigs.value): visual = glx.glXGetVisualFromFBConfig( self.d, fbconfigs[i]).contents if not visual: continue pict_format = glx.XRenderFindVisualFormat( self.d, visual.visual).contents if not pict_format: continue fbconfig = fbconfigs[i] if pict_format.direct.alphaMask > 0: break if not fbconfig: print("No matching FB config found") #/* Create a colormap - only needed on some X clients, eg. IRIX */ cmap = xlib.XCreateColormap(self.d, self.root, visual.visual, AllocNone) attr = xlib.XSetWindowAttributes() attr.colormap = cmap attr.background_pixmap = 0 attr.border_pixmap = 0 attr.border_pixel = 0 attr.event_mask = (StructureNotifyMask | EnterWindowMask | LeaveWindowMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | OwnerGrabButtonMask | KeyPressMask | KeyReleaseMask) attr_mask = ( # CWBackPixmap| CWColormap | CWBorderPixel | CWEventMask) self.window = xlib.XCreateWindow(self.d, self.root, x, y, w, h, 0, visual.depth, 1, visual.visual, attr_mask, byref(attr)) else: # normal EGL created context self.window = xlib.XCreateSimpleWindow(self.d, self.root, x, y, w, h, 1, 0, 0) s = ctypes.create_string_buffer(b'WM_DELETE_WINDOW') self.WM_DELETE_WINDOW = ctypes.c_ulong( xlib.XInternAtom(self.d, s, 0)) # set window title title = ctypes.c_char_p(self.window_title) title_length = ctypes.c_int(len(self.window_title)) wm_name_atom = ctypes.c_ulong( xlib.XInternAtom(self.d, ctypes.create_string_buffer(b'WM_NAME'), 0)) string_atom = ctypes.c_ulong( xlib.XInternAtom(self.d, ctypes.create_string_buffer(b'STRING'), 0)) xlib.XChangeProperty(self.d, self.window, wm_name_atom, string_atom, 8, xlib.PropModeReplace, title, title_length) if (w == self.width and h == self.height) or (self.display_config & DISPLAY_CONFIG_FULLSCREEN): # set full-screen. Messy c function calls! wm_state = ctypes.c_ulong( xlib.XInternAtom(self.d, b'_NET_WM_STATE', 0)) fullscreen = ctypes.c_ulong( xlib.XInternAtom(self.d, b'_NET_WM_STATE_FULLSCREEN', 0)) fullscreen = ctypes.cast(ctypes.pointer(fullscreen), ctypes.c_char_p) XA_ATOM = 4 xlib.XChangeProperty(self.d, self.window, wm_state, XA_ATOM, 32, xlib.PropModeReplace, fullscreen, 1) self.width, self.height = w, h if self.display_config & DISPLAY_CONFIG_HIDE_CURSOR: black = xlib.XColor() black.red = 0 black.green = 0 black.blue = 0 noData = ctypes.c_char_p(bytes([0, 0, 0, 0, 0, 0, 0, 0])) bitmapNoData = xlib.XCreateBitmapFromData( self.d, self.window, noData, 8, 8) invisibleCursor = xlib.XCreatePixmapCursor( self.d, bitmapNoData, bitmapNoData, black, black, 0, 0) xlib.XDefineCursor(self.d, self.window, invisibleCursor) #TODO add functions to xlib for these window manager libx11 functions #self.window.set_wm_name('pi3d xlib window') #self.window.set_wm_icon_name('pi3d') #self.window.set_wm_class('draw', 'XlibExample') xlib.XSetWMProtocols(self.d, self.window, self.WM_DELETE_WINDOW, 1) #self.window.set_wm_hints(flags = Xutil.StateHint, # initial_state = Xutil.NormalState) #self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize # | Xutil.PMinSize), # min_width = 20, # min_height = 20) xlib.XSelectInput(self.d, self.window, KeyPressMask | KeyReleaseMask) xlib.XMapWindow(self.d, self.window) #xlib.XMoveWindow(self.d, self.window, x, y) #TODO this has to happen later. Works after rendering first frame. Check when if self.use_glx: dummy = c_int() if not glx.glXQueryExtension(self.d, byref(dummy), byref(dummy)): print("OpenGL not supported by X server\n") dummy_glx_context = ctypes.cast(0, glx.GLXContext) self.render_context = glx.glXCreateNewContext( self.d, fbconfig, glx.GLX_RGBA_TYPE, dummy_glx_context, True) if not self.render_context: print("Failed to create a GL context\n") if not glx.glXMakeContextCurrent( self.d, self.window, self.window, self.render_context): print("glXMakeCurrent failed for window\n") else: self.surface = openegl.eglCreateWindowSurface( self.display, self.config, self.window, 0) if not self.use_glx: assert self.surface != EGL_NO_SURFACE and self.surface is not None r = openegl.eglMakeCurrent(self.display, self.surface, self.surface, self.context) assert r #Create viewport opengles.glViewport(GLint(0), GLint(0), GLsizei(w), GLsizei(h))
("nSequenceCounterUnderflow", ct.c_ulong), # nSequenceCounter can # underflow (for indefinitely long Sequences: AlpProjStartCont); # nSequenceCounterUnderflow is 0 before, and non-null afterwards ("nFrameCounter", ct.c_ulong), # Frames left inside current iteration ("nPictureTime", ct.c_ulong), # micro seconds of each frame; this is # reported, because the picture time of the original sequence could # already have changed in between ("nFramesPerSubSequence", ct.c_ulong), # Each sequence iteration # displays this number of frames. It is reported to the user just for # convenience, because it depends on different parameters. */ ("nFlagse", ct.c_ulong) ] # may be a combination of ALP_FLAG_SEQUENCE_ABORTING | SEQUENCE_INDEFINITE | QUEUE_IDLE | FRAME_FINISHED ALP_FLAG_QUEUE_IDLE = ct.c_ulong(1) ALP_FLAG_SEQUENCE_ABORTING = ct.c_ulong(2) ALP_FLAG_SEQUENCE_INDEFINITE = ct.c_ulong( 4) #AlpProjStartCont: this sequence runs indefinitely long, until aborted ALP_FLAG_FRAME_FINISHED = ct.c_ulong( 8) #illumination of last frame finished, picture time still progressing ALP_FLAG_RSVD0 = ct.c_ulong(16) # reserved ALP_ERRORS = { 1001: 'The specified ALP device has not been found or is not ready.', 1002: 'The ALP device is not in idle state.', 1003: 'The specified ALP device identifier is not valid.', 1004: 'The specified ALP device is already allocated.', 1005: 'One of the parameters is invalid.', 1006: 'Error accessing user data.', 1007: 'The requested memory is not available (full?).',
def configure(self, SampleLength, SecondsPerPoint, DutyCycle=0.9, MaxCounts=1e7, RWTimeout=1.0): if hasattr(self, '_CITask') or hasattr(self, '_COTask'): self.clear() f = 1. / SecondsPerPoint # nidaq Tasks self._COTask = ctypes.c_ulong() self._CITask = ctypes.c_ulong() CHK(dll.DAQmxCreateTask('', ctypes.byref(self._COTask))) CHK(dll.DAQmxCreateTask('', ctypes.byref(self._CITask))) # ctr1 generates a continuous square wave with given duty cycle. This serves simultaneously # as sampling clock for AO (update DAC at falling edge), and as gate for counter (count between # rising and falling edge) CHK( dll.DAQmxCreateCOPulseChanFreq(self._COTask, self._CounterOut, '', DAQmx_Val_Hz, DAQmx_Val_Low, ctypes.c_double(0), ctypes.c_double(f), ctypes.c_double(DutyCycle))) # ctr0 is used to count photons. Used to count ticks in N+1 gates CHK( dll.DAQmxCreateCIPulseWidthChan( self._CITask, self._CounterIn, '', ctypes.c_double(0), ctypes.c_double(MaxCounts * DutyCycle / f), DAQmx_Val_Ticks, DAQmx_Val_Rising, '')) CHK( dll.DAQmxSetCIPulseWidthTerm(self._CITask, self._CounterIn, self._CounterOut + 'InternalOutput')) CHK( dll.DAQmxSetCICtrTimebaseSrc(self._CITask, self._CounterIn, self._TickSource)) CHK( dll.DAQmxCfgImplicitTiming(self._COTask, DAQmx_Val_ContSamps, ctypes.c_ulonglong(SampleLength))) CHK( dll.DAQmxCfgImplicitTiming(self._CITask, DAQmx_Val_FiniteSamps, ctypes.c_ulonglong(SampleLength))) # read samples from beginning of acquisition, do not overwrite CHK(dll.DAQmxSetReadRelativeTo(self._CITask, DAQmx_Val_CurrReadPos)) CHK(dll.DAQmxSetReadOffset(self._CITask, 0)) CHK( dll.DAQmxSetReadOverWrite(self._CITask, DAQmx_Val_DoNotOverwriteUnreadSamps)) self._CIData = numpy.empty((SampleLength, ), dtype=numpy.uint32) self._CINread = ctypes.c_int32() self._SampleLength = SampleLength self._TaskTimeout = 4 * SampleLength / f self._RWTimeout = RWTimeout
def __init__(self, width=80, stdout=sys.stdout, stderr=sys.stderr): _Console.__init__(self, width, stdout, stderr) windll.Kernel32.GetStdHandle.restype = c_ulong self._handle = windll.Kernel32.GetStdHandle(c_ulong(0xfffffff5))
def _mock_edna(): # This function will mock all the methods that were used in the dna_dll. # It's necessary so that documentation can be automatically created. dna_dll = Mock() attrs = { 'DnaGetHistAvgUTC.return_value': c_ulong(1), 'DnaGetHistInterpUTC.return_value': c_ulong(1), 'DnaGetHistMinUTC.return_value': c_ulong(1), 'DnaGetHistMaxUTC.return_value': c_ulong(1), 'DnaGetHistSnapUTC.return_value': c_ulong(1), 'DnaGetHistRawUTC.return_value': c_ulong(1), 'DoesIdExist.return_value': c_ulong(1), 'DnaGetHSHistRawUTC.return_value': c_ulong(1), 'DnaGetNextHSHistUTC.return_value': c_ulong(1), 'DnaGetPointEntry.return_value': c_ulong(1), 'DnaGetNextPointEntry.return_value': c_ulong(1), 'DNAGetRTFull.return_value': c_ulong(1), 'DnaSelectPoint.return_value': c_ulong(1), 'StringToUTCTime.return_value': 1, 'DnaGetServiceEntry.return_value': c_ulong(1), 'DnaGetNextServiceEntry.return_value': c_ulong(1), 'DnaHistAppendValues.return_value': c_ulong(1), 'DnaHistUpdateInsertValues.return_value': c_ulong(1), 'DnaCancelHistRequest.return_value': None, 'DnaGetNextHistSmallUTC.return_value': c_ulong(1) } dna_dll.configure_mock(**attrs) return dna_dll
file_selections = [] is_windows = False is_mac = False is_terminal = False is_linux = False cached_title = None if app.platform == "windows": is_windows = True import ctypes GetUserNameEx = ctypes.windll.secur32.GetUserNameExW NameDisplay = 3 size = ctypes.pointer(ctypes.c_ulong(0)) GetUserNameEx(NameDisplay, None, size) nameBuffer = ctypes.create_unicode_buffer(size.contents.value) GetUserNameEx(NameDisplay, nameBuffer, size) one_drive_path = os.path.expanduser(os.path.join("~", "OneDrive")) # this is probably not the correct way to check for onedrive, quick and dirty if os.path.isdir(os.path.expanduser(os.path.join("~", r"OneDrive\Desktop"))): default_folder = os.path.join("~", "Desktop") ctx.lists["user.file_manager_directory_remap"] = { "Desktop": os.path.join(one_drive_path, "Desktop"), "Documents": os.path.join(one_drive_path, "Documents"), "Downloads": os.path.join(user_path, "Downloads"), "Music": os.path.join(user_path, "Music"),
def _asm_func(self, restype=None, argtypes=(), byte_code=[]): import ctypes byte_code = bytes.join(b'', byte_code) address = None if is_windows: # Allocate a memory segment the size of the byte code, and make it executable size = len(byte_code) # Alloc at least 1 page to ensure we own all pages that we want to change protection on if size < 0x1000: size = 0x1000 MEM_COMMIT = ctypes.c_ulong(0x1000) PAGE_READWRITE = ctypes.c_ulong(0x4) pfnVirtualAlloc = ctypes.windll.kernel32.VirtualAlloc pfnVirtualAlloc.restype = ctypes.c_void_p address = pfnVirtualAlloc(None, ctypes.c_size_t(size), MEM_COMMIT, PAGE_READWRITE) if not address: raise Exception("Failed to VirtualAlloc") # Copy the byte code into the memory segment memmove = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)(ctypes._memmove_addr) if memmove(address, byte_code, size) < 0: raise Exception("Failed to memmove") # Enable execute permissions PAGE_EXECUTE = ctypes.c_ulong(0x10) old_protect = ctypes.c_ulong(0) pfnVirtualProtect = ctypes.windll.kernel32.VirtualProtect res = pfnVirtualProtect(ctypes.c_void_p(address), ctypes.c_size_t(size), PAGE_EXECUTE, ctypes.byref(old_protect)) if not res: raise Exception("Failed VirtualProtect") # Flush Instruction Cache # First, get process Handle if not self.prochandle: pfnGetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess pfnGetCurrentProcess.restype = ctypes.c_void_p self.prochandle = ctypes.c_void_p(pfnGetCurrentProcess()) # Actually flush cache res = ctypes.windll.kernel32.FlushInstructionCache(self.prochandle, ctypes.c_void_p(address), ctypes.c_size_t(size)) if not res: raise Exception("Failed FlushInstructionCache") else: # Allocate a memory segment the size of the byte code size = len(byte_code) pfnvalloc = ctypes.pythonapi.valloc pfnvalloc.restype = ctypes.c_void_p address = pfnvalloc(ctypes.c_size_t(size)) if not address: raise Exception("Failed to valloc") # Mark the memory segment as writeable only if not self.is_selinux_enforcing: WRITE = 0x2 if ctypes.pythonapi.mprotect(ctypes.c_void_p(address), size, WRITE) < 0: raise Exception("Failed to mprotect") # Copy the byte code into the memory segment if ctypes.pythonapi.memmove(ctypes.c_void_p(address), byte_code, ctypes.c_size_t(size)) < 0: raise Exception("Failed to memmove") # Mark the memory segment as writeable and executable only if not self.is_selinux_enforcing: WRITE_EXECUTE = 0x2 | 0x4 if ctypes.pythonapi.mprotect(ctypes.c_void_p(address), size, WRITE_EXECUTE) < 0: raise Exception("Failed to mprotect") # Cast the memory segment into a function functype = ctypes.CFUNCTYPE(restype, *argtypes) fun = functype(address) return fun, address
def init(env): global win_original_attributes global win_original_cursor_info if is_windows: stdout_handle = ctypes.windll.kernel32.GetStdHandle( ctypes.c_ulong(-11)) cbuf = CONSOLE_SCREEN_BUFFER_INFO() ctypes.windll.kernel32.GetConsoleScreenBufferInfo( stdout_handle, ctypes.byref(cbuf)) win_original_attributes = cbuf.wAttributes win_original_cursor_info = CONSOLE_CURSOR_INFO() ctypes.windll.kernel32.GetConsoleCursorInfo( stdout_handle, ctypes.byref(win_original_cursor_info)) old_output_mode = ctypes.c_uint32() ctypes.windll.kernel32.GetConsoleMode(stdout_handle, ctypes.byref(old_output_mode)) ctypes.windll.kernel32.SetConsoleMode( stdout_handle, 1 | # ENABLE_PROCESSED_OUTPUT 2 # ENABLE_WRAP_AT_EOL_OUTPUT # 4 | # ENABLE_VIRTUAL_TERMINAL_PROCESSING win 10 only : ( # 8 # DISABLE_NEWLINE_AUTO_RETURN win 10 only : ( ) atexit.register(lambda: ctypes.windll.kernel32.SetConsoleMode( stdout_handle, old_output_mode.value)) old_input_mode = ctypes.c_uint32() stdin_handle = ctypes.windll.kernel32.GetStdHandle(ctypes.c_ulong(-10)) ctypes.windll.kernel32.GetConsoleMode(stdin_handle, ctypes.byref(old_input_mode)) res = ctypes.windll.kernel32.SetConsoleMode( stdin_handle, 0 # 0x200 # ENABLE_VIRTUAL_TERMINAL_INPUT win 10 only : ( ) atexit.register(lambda: ctypes.windll.kernel32.SetConsoleMode( stdin_handle, old_input_mode.value)) else: # Unix import os, signal, termios, tty if stdin_is_tty: stdin_fd = sys.stdin.fileno() orig = termios.tcgetattr(stdin_fd) atexit.register( lambda: termios.tcsetattr(stdin_fd, termios.TCSAFLUSH, orig)) tty.setcbreak(stdin_fd) def on_tstp_sig(signum, stack_frame): unix_in_tstp_signal = True on_exit_common() signal.signal(signum, signal.SIG_DFL) os.kill(os.getpid(), signum) def on_cont_sig(signum, stack_frame): if stdin_is_tty: stdin_fd = sys.stdin.fileno() tty.setcbreak(stdin_fd) unix_in_tstp_signal = False if len(stored_chars): # thread will eat the first char # pressed after Ctrl-z # TODO: fix this? hard since select/poll # doesn't work right on cygwin. stored_chars.pop() unix_screen_is_reset = True signal.signal(signal.SIGTSTP, on_tstp_sig) signal.signal(signal.SIGTSTP, on_tstp_sig) signal.signal(signal.SIGCONT, on_cont_sig) def on_exit_common(): home_cursor() cursor_down(env.hdr.screen_height_units) reset_color() show_cursor() atexit.register(on_exit_common) hide_cursor() start_char_loop_thread()
def _poll_iocompletion_port(self): # Watch the IO Completion port for status self._spawned_procs = {} countdowntokill = 0 self.debug("start polling IO completion port") while True: msgid = c_ulong(0) compkey = c_ulong(0) pid = c_ulong(0) portstatus = winprocess.GetQueuedCompletionStatus( self._io_port, byref(msgid), byref(compkey), byref(pid), 5000) # If the countdowntokill has been activated, we need to check # if we should start killing the children or not. if countdowntokill != 0: diff = datetime.now() - countdowntokill # Arbitrarily wait 3 minutes for windows to get its act together # Windows sometimes takes a small nap between notifying the # IO Completion port and actually killing the children, and we # don't want to mistake that situation for the situation of an unexpected # parent abort (which is what we're looking for here). if diff.seconds > self.MAX_IOCOMPLETION_PORT_NOTIFICATION_DELAY: print( "WARNING | IO Completion Port failed to signal " "process shutdown", file=sys.stderr) print( "Parent process %s exited with children alive:" % self.pid, file=sys.stderr) print("PIDS: %s" % ', '.join( [str(i) for i in self._spawned_procs]), file=sys.stderr) print( "Attempting to kill them, but no guarantee of success", file=sys.stderr) self.kill() self._process_events.put({self.pid: 'FINISHED'}) break if not portstatus: # Check to see what happened errcode = winprocess.GetLastError() if errcode == winprocess.ERROR_ABANDONED_WAIT_0: # Then something has killed the port, break the loop print("IO Completion Port unexpectedly closed", file=sys.stderr) self._process_events.put({self.pid: 'FINISHED'}) break elif errcode == winprocess.WAIT_TIMEOUT: # Timeouts are expected, just keep on polling continue else: print( "Error Code %s trying to query IO Completion Port, " "exiting" % errcode, file=sys.stderr) raise WinError(errcode) break if compkey.value == winprocess.COMPKEY_TERMINATE.value: self.debug("compkeyterminate detected") # Then we're done break # Check the status of the IO Port and do things based on it if compkey.value == winprocess.COMPKEY_JOBOBJECT.value: if msgid.value == winprocess.JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO: # No processes left, time to shut down # Signal anyone waiting on us that it is safe to shut down self.debug("job object msg active processes zero") self._process_events.put({self.pid: 'FINISHED'}) break elif msgid.value == winprocess.JOB_OBJECT_MSG_NEW_PROCESS: # New Process started # Add the child proc to our list in case our parent flakes out on us # without killing everything. if pid.value != self.pid: self._spawned_procs[pid.value] = 1 self.debug( "new process detected with pid value: %s" % pid.value) elif msgid.value == winprocess.JOB_OBJECT_MSG_EXIT_PROCESS: self.debug("process id %s exited normally" % pid.value) # One process exited normally if pid.value == self.pid and len( self._spawned_procs) > 0: # Parent process dying, start countdown timer countdowntokill = datetime.now() elif pid.value in self._spawned_procs: # Child Process died remove from list del (self._spawned_procs[pid.value]) elif msgid.value == winprocess.JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS: # One process existed abnormally self.debug("process id %s exited abnormally" % pid.value) if pid.value == self.pid and len( self._spawned_procs) > 0: # Parent process dying, start countdown timer countdowntokill = datetime.now() elif pid.value in self._spawned_procs: # Child Process died remove from list del self._spawned_procs[pid.value] else: # We don't care about anything else self.debug("We got a message %s" % msgid.value) pass